↓Skip to main content
  1. LeetCode/

LeetCode 92: Reverse Linked List II

·2 mins· ·
LeetCode Medium Linked-List
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-26

  • Submissions: Accepted ×2 → Wrong Answer → Accepted

Approach
#

Walk a dummy to node left - 1, pre (the dummy itself when left = 1). cur = pre.next starts the range and ends up as its tail. Then, right - left times, take the node after cur and insert it right after pre (Linked List Template 2).

My Attempts
#

The first two submissions passed. A later rewrite split the list into three parts, reversed the middle and reconnected, but reconnected the range’s original first node middle (the tail after reversing) instead of the reversed head prev. For [8, 4], left = 1, right = 2 the answer is [4, 8]; it returned [8].

Solution
#

class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        dummy = ListNode(0, head)
        pre = dummy
        for _ in range(left - 1):
            pre = pre.next
        cur = pre.next
        for _ in range(right - left):
            nxt = cur.next                           # 拿出 cur 後面那個
            cur.next = nxt.next
            nxt.next = pre.next                      # 插到 pre 後面
            pre.next = nxt
        return dummy.next

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • With left = 1 the head changes, so start from a dummy.
  • cur stays the same node, moving further back each time.

Interview Explanation
#

I walk a dummy to the node before left, then repeatedly move the next node in the range to its front, right - left times, which reverses the range in one pass. Time O(n).

Related

LeetCode 109: Convert Sorted List to Binary Search Tree
·2 mins
LeetCode Medium Linked-List Divide-and-Conquer Tree Binary-Search-Tree
LeetCode 109 note: take the middle found with fast and slow pointers as the root and recurse on both halves; the height balances naturally
LeetCode 116: Populating Next Right Pointers in Each Node
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search
LeetCode 116 note: in a perfect binary tree, walk each level through the next pointers already set on the level above: left.next = right and right.next = next.left, with O(1) extra space
LeetCode 148: Sort List
·2 mins
LeetCode Medium Linked-List Two-Pointers Divide-and-Conquer Sorting
LeetCode 148 note: split in half with fast and slow pointers, sort each half recursively and merge; O(n log n) with no extra array
LeetCode 138: Copy List with Random Pointer
·2 mins
LeetCode Medium Hash-Table Linked-List
LeetCode 138 note: the first pass maps old nodes to new ones, the second wires next and random; seed the dictionary with None -> None
LeetCode 143: Reorder List
·2 mins
LeetCode Medium Linked-List Two-Pointers Stack Recursion
LeetCode 143 note: find the middle, reverse the second half, and merge the two alternately, cutting the first half’s tail
LeetCode 82: Remove Duplicates from Sorted List II
·2 mins
LeetCode Medium Linked-List Two-Pointers
LeetCode 82 note: every duplicated value goes, the head included, so use a dummy; on a duplicate skip to the next different value and link prev.next past it