DrawCode Algorithm Visualizer • Linked List • Easy

Reverse Linked List

Tags: Linked List, In-place, Pointers, Iteration

1. One-Liner

Reverse a singly linked list by rewiring next pointers so traversal order flips—iteratively with prev/cur pointers.


2. The Problem It Solves

Needed for palindrome checks (reverse half), stack-free undo of order, and many follow-up problems.


3. The Core Idea

Carry prev (starts null) and walk cur; save next, point cur→prev, slide forward—classic three-pointer dance.


4. How It Works (Step-by-Step)

StepAction
1prev=null, cur=head
2While cur: nxt=cur.next; cur.next=prev
3prev=cur; cur=nxt
4Return prev as new head

5. Dry Run Example

1→2→3: after loop prev points to new head 3→2→1→null.


6. Key Properties

PropertyDetail
In-placeO(1) extra for pointers
StableN/A—order reversed
RecursionO(n) stack possible

7. Where It Is Used

Domain / SystemUse
EditorsUndo lists
Kernelsmbuf chains
InterviewsPalindrome, add numbers

8. Interview Tips

Handle empty and single node. Mention recursive version if asked—same logic, implicit stack.


9. Comparison with Other Algorithms

TechniqueNotes
Copy to arrayO(n) space
Stack of nodesExtra space
Doubly linkedEasier but different structure

10. Complexity

----
TimeO(n)
SpaceO(1) iterative

Implementation Example (PYTHON)

def reverse_list(head):
    prev, cur = None, head
    while cur:
        nxt = cur.next
        cur.next = prev
        prev, cur = cur, nxt
    return prev

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer