#92

Reverse Linked List II

Medium Reverse a sublist in place

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Reverse only the nodes from position left to position right (1-indexed) in one pass.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: reverse only positions left..right.

Why this works: stand before the segment, then repeatedly move the node after the segment's first node to the front of the segment.

Example: 1→2→3→4→5, left 2, right 4

move 3 to front: 1→3→2→4→5move 4 to front: 1→4→3→2→5

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Losing the connection to the part after right by not linking cur.next correctly during each move.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Linked List
🎬 Episodes 40–45: video coming soon