Reverse Linked List Visualization & Animation

Reverses a singly linked list in-place by relinking pointers; O(n) time, O(1) space.

## What is it? Reverse a singly linked list so that the last node becomes the head. An essential linked list operation used as a building block in many problems. ## How it works **Iterative (in-place):** - Use three pointers: `prev = null`, `curr = head`, `next` - In each step: save `curr.next`, set `curr.next = prev`, advance `prev = curr`, advance `curr = next` - When `curr == null`, `prev` is the new head **Recursive:** - Base case: empty or single node - Reverse rest of list, then `head.next.next = head`, `head.next = null` - Return new head (last node of original) ## When to use - Palindrome Linked List (reverse second half, compare) - Reverse sublist or groups of k nodes - Reorder list problems ## Key Points - Iterative: O(n) time, O(1) space — preferred - Recursive: O(n) time, O(n) call stack space - Always return the new head (the original tail)

Category: algorithms

Difficulty: beginner

Time Complexity: O(n)

Space Complexity: O(1)

Reverse Linked List

beginner

Reverses a singly linked list in-place by relinking pointers; O(n) time, O(1) space.

Initializing
prev
null
curr
1
next
2
nullnull138472215634921415675NULLprevcurrnexthead
Initialize: prev = null, curr = node-0 (1). We will flip each link one by one until curr is null.