easyLinked ListPure DSA~15 min
Reverse Event Log
A debugger replays an in-memory event log to the developer. The log is stored as a singly linked list with the oldest event at head, but the developer wants the most recent first. Reverse the list in place — the debugger has tight memory and cannot allocate a second copy.
Problem
Given the head of a singly linked list, return the head of the same list reversed in place. The list nodes must be reused (no new node allocation).
Input
The head pointer of a singly linked list with 0 ≤ n ≤ 10^5 nodes. Each node stores an integer value.
Output
The head pointer of the reversed list.
Constraints
- 0 ≤ n ≤ 100,000
- Must reverse in place — no new node allocation
- O(1) extra space target
Examples
Example 1
Input
head → 1 → 2 → 3 → 4 → null
Output
head → 4 → 3 → 2 → 1 → null
Each next pointer is flipped to point backward.
Example 2
Input
head → null
Output
head → null
Empty list stays empty.