loading

Reverse Linked List II — Step-by-Step Visualization

mediumLeetCode #92Linked List

Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.

Algorithm Pattern

Sublist Reversal

Key Idea

Use a dummy node and repeatedly move the next node to the front of the reversed segment.

Step-by-Step Approach

  1. Create dummy node before head
  2. Advance to node just before 'left'
  3. Repeatedly insert current.next at the front of the reversed segment
  4. Stop after (right-left) insertions

Common Gotchas

  • Use a dummy node to cleanly handle the case where left=1 (reversal starts at head).
  • The insertion technique avoids needing to track multiple pointers explicitly.

Related Problems