AlgoFlow
intermediateLinked Lists

Reverse a linked list

Given the head of a singly linked list, reverse the list so the last node becomes the head. Do it in place, using O(1) extra memory.

Examples

input 1 → 2 → 3 → 4

output 4 → 3 → 2 → 1

Every next pointer flips direction.

input 1 (single node)

output 1

A one-node list is its own reverse.

Hints

Try the problem first. Hints are revealed one at a time so you keep thinking.

Solution

The written approach, complexity analysis and follow-ups stay hidden until you ask for them.