leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

92. 反转链表 II

Open buuing opened this issue 3 years ago • 0 comments

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。  

示例 1:

image

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

示例 2:

输入:head = [5], left = 1, right = 1
输出:[5]

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

进阶: 你可以使用一趟扫描完成反转吗?


来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reverse-linked-list-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。




const reverseBetween = (head, left, right) => {
  let res = prev = { next: head }, index = 1
  let start, end, newHead, curr = head
  while (curr) {
    let next = curr.next
    if (index === left) {
      start = prev
      end = curr
      newHead = curr
    }
    if (index > left) {
      curr.next = newHead
      newHead = curr
    }
    if (index === right) {
      start.next = newHead
      end.next = next
      break
    }
    prev = curr
    curr = next
    index++
  }
  return res.next
}

buuing avatar Jun 07 '21 16:06 buuing