add: K个一组翻转链表
This commit is contained in:
49
src/list/reverse-nodes-in-k-group.js
Normal file
49
src/list/reverse-nodes-in-k-group.js
Normal 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
|
||||
}
|
Reference in New Issue
Block a user