-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0025-reverse-nodes-in-k-group.js
49 lines (46 loc) · 1.19 KB
/
0025-reverse-nodes-in-k-group.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 25. Reverse Nodes in k-Group
* https://leetcode.com/problems/reverse-nodes-in-k-group/
* Difficulty: Hard
*
* Given the head of a linked list, reverse the nodes of the list k at a time,
* and return the modified list.
*
* k is a positive integer and is less than or equal to the length of the linked
* list. If the number of nodes is not a multiple of k then left-out nodes, in
* the end, should remain as it is.
*
* You may not alter the values in the list's nodes, only nodes themselves may
* be changed.
*/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var reverseKGroup = function(head, k) {
const result = new ListNode(null, head);
const stack = [];
let tail = result;
while (head) {
for (let i = 0; i < k && head; i++) {
stack.push(head);
head = head.next;
}
if (stack.length === k) {
while (stack.length) {
tail.next = stack.pop();
tail = tail.next;
}
tail.next = head;
}
}
return result.next;
};