Reversing a linked list

The classic interview question, animated: flip one arrow at a time with prev and curr pointers.

A singly linked list is just nodes pointing forward. Reversing it means making every arrow point backward, and the trick is doing it in one walk, with no extra memory.

Two pointers do the work: curr walks the list, prev trails one node behind, and at each step you flip curr's arrow to point at prev before advancing both. When curr walks off the end, the old tail is the new head. One pass, O(n) time, O(1) space, which is exactly why interviewers love it.

Remember this

  • Flip one arrow per step as prev and curr walk the list
  • One pass, no extra memory: O(n) time, O(1) space
  • The old tail becomes the new head

Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.

Ask your own question →

Keep learning