Skip to content

Instantly share code, notes, and snippets.

@j-quelly
Created January 3, 2019 18:02
Show Gist options
  • Save j-quelly/e52b9cb431bf835343f18839ee42bbe0 to your computer and use it in GitHub Desktop.
Save j-quelly/e52b9cb431bf835343f18839ee42bbe0 to your computer and use it in GitHub Desktop.
// Definition for singly-linked list:
// function ListNode(x) {
// this.value = x;
// this.next = null;
// }
//
function reverseNodesInKGroups(l, k) {
let temp = k;
let current = l;
const results = [];
let temparr = [];
while (temp && current) {
if (current.value) {
temparr.unshift(current.value);
}
current = current.next;
temp--;
if (temp === 0) {
temp = k;
results.push(...temparr);
temparr = [];
}
}
results.push(...temparr.reverse());
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment