var reverseBetween = function (head, left, right) { const dummyHead = new ListNode() dummyHead.next = head let keyPrev = dummyHead for (let i = 1; i < left; ++i) { keyPrev = keyPrev.next } let cur = keyPrev.next let keyTail = cur let newLink = null let steps = right - left + 1 while (cur && steps--) { const remainLink = cur.next cur.next = newLink newLink = cur keyTail.next = remainLink cur = remainLink } keyPrev.next = newLink return dummyHead.next }