add: K个一组翻转链表

This commit is contained in:
2020-05-16 19:50:31 +08:00
parent 5593f3e2c7
commit 55ff39a1c3
4 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,49 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
export const reverseKGroup = function (head, k) {
let sum = 0
let start = head
let newStart = head
let first = true
const last = []
while (head) {
if (++sum === k) {
const headNext = head.next
last.push(start)
let next = head.next
for (let i = 0; i < sum - 1; i++) {
const tmp = start.next
start.next = next
next = start
start = tmp
}
start.next = next
if (first) {
newStart = start
first = false
} else {
const la = last.shift()
la.next = head
}
sum = 0
start = headNext
head = headNext
} else {
head = head.next
}
}
return newStart
}