js-practice/src/list/reverse-nodes-in-k-group.js

48 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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 res = head // 返回值:如果进行过翻转,则为第一次翻转的最后一个结点
const queue = [] // 使用队列方便连接上一次翻转的链表最大长度为2
while (head) { // 遍历一次链表
if (++sum === k) { // 如果经过了k个结点则翻转从start到head的一段结点
const headNext = head.next
queue.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 (queue.length === 1) { // 判断是否为第一次翻转
res = start
} else {
const la = queue.shift() // 连接上一次翻转的链表
la.next = head
}
sum = 0 // 重置计数
start = headNext
head = headNext
} else {
head = head.next
}
}
return res
}