Skip to content

visual walkthrough

Reverse Linked List II

MediumIn-place Reversal (Sublist)Reported at: MetaAmazonMicrosoft+3

Reversal of a sub-range with correct reconnection.

Solve on LeetCode

The idea

Only a slice of the list is reversed, so the nodes around it must be reconnected afterwards. Keep a pointer to the node just before the slice.

Then pull each later node of the slice to the front, one at a time. After right − left moves the slice is reversed and still connected at both ends.

Complexity

approachtimespace
Copy values and write backO(n)O(right − left)
Move nodes to the frontO(n)O(1)

More walkthroughs