1. /**
    2. * Definition for singly-linked list.
    3. * function ListNode(val, next) {
    4. * this.val = (val===undefined ? 0 : val)
    5. * this.next = (next===undefined ? null : next)
    6. * }
    7. */
    8. /**
    9. * @param {ListNode} head
    10. * @param {number} left
    11. * @param {number} right
    12. * @return {ListNode}
    13. */
    14. var reverseBetween = function (head, left, right) {
    15. const node = new ListNode(null)
    16. node.next = head
    17. let pre = node
    18. for (let i = 0; i < left - 1; ++i) {
    19. pre = pre.next
    20. }
    21. let cur = pre.next
    22. for (let i = 0; i < right - left; ++i) {
    23. const next = cur.next
    24. cur.next = next.next
    25. next.next = pre.next
    26. pre.next = next
    27. }
    28. return node.next
    29. };