Created
January 3, 2019 18:02
-
-
Save j-quelly/e52b9cb431bf835343f18839ee42bbe0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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