-
Notifications
You must be signed in to change notification settings - Fork 0
/
25.reverse-nodes-in-k-group.js
70 lines (58 loc) · 1.38 KB
/
25.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* @lc app=leetcode id=25 lang=javascript
*
* [25] Reverse Nodes in k-Group
*/
// @lc code=start
/**
* 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) {
let dummy = new ListNode(0, head);
let groupPrev = dummy;
while(true){
let kth = getKth(groupPrev, k);
if(!kth) break;
let groupNext = kth.next;
let prev = kth.next
let curr = groupPrev.next;
while(curr !== groupNext){
var tmp = curr.next;
curr.next = prev
prev=curr;
curr = tmp
}
tmp = groupPrev.next;
groupPrev.next = kth;
groupPrev = tmp;
}
function getKth(curr, k){
while(curr && k > 0){
curr = curr.next;
k -=1
}
return curr
}
return dummy.next;
};
// @lc code=end
function ListNode(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
let l1 = new ListNode(1);
l1.next = new ListNode(2);
l1.next.next = new ListNode(3);
l1.next.next.next = new ListNode(4);
l1.next.next.next.next = new ListNode(5);
let k = 2
reverseKGroup(l1, k)