QuestionsDSA

Reverse a Linked List

Linked List / Fast-SlowEasyDSA

Reverse a singly linked list and return the new head.

What it tests

Pointer manipulation without losing the rest of the list.

Approach & answer

Walk the list carrying prev, cur, next. Save next, point cur.next back to prev, advance both. The key is caching `next` before you overwrite the pointer. Iterative is O(1) space; recursion is elegant but O(n) stack. Draw three boxes and move the arrows one at a time — the classic mistake is reassigning `cur.next = prev` before stashing `cur.next`, which severs the rest of the list. `prev` starts at null so the original head correctly becomes the new tail pointing at null. Reversal is a building block for palindrome-linked-list and reverse-nodes-in-k-group.

Use this technique when

In-place linked list rewiring → three-pointer walk (prev/cur/next).

Complexity

Time O(n) · Space O(1)

References

js