Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Reverse Linked List

LeetCode #206Easy
Flip every arrow · prev / curr / next

Given the head of a singly linked list, reverse the list and return the new head.

Asked atAmazonMicrosoftAppleGoogle
step 1 / 17
1
2
3
4
5
[0][1][2][3][4]
Iterative · three pointers
1prev ← ∅; curr ← head
2while curr:
3 nextcurr.next // save the rest
4 curr.nextprev // flip the arrow
5 prevcurr; currnext
6return prev // new head
state
  • prev
  • currhead (1)

line 1Reverse the list in place. Keep three pointers: prev (the part already reversed, starts at ∅), curr (the node we are flipping), and next (a temporary so we don’t lose the tail).