var reverseKGroup = function (head, k) { const checkReverseAble = (link) => { // 若长度小于 0 则 let cur = link let count = 0 while (cur) { ++count if (count >= k) { returntrue } cur = cur.next } returnfalse } const reverseLink = (link) => { const origHead = link let newLink = null let remainLink = link let steps = k while (remainLink && steps--) { const newHead = remainLink remainLink = remainLink.next newHead.next = newLink
origHead.next = remainLink newLink = newHead } return newLink } const dummyHead = new ListNode() dummyHead.next = head let cur = dummyHead while (cur.next && checkReverseAble(cur.next)) { const nextNode = cur.next cur.next = reverseLink(nextNode) cur = nextNode } return dummyHead.next }