Reverse Linked List — Linked List

Flip a chain of linked items so the order runs backwards, then return the new first item; the chain may be empty.

Linked List · Easy

The problem

A music player stores its queue as a chain of songs, and you want to play it backwards. Given the head of a singly linked list, turn the list around so the last node comes first, and return the head of the turned-around list.

The story

Your road-trip playlist is stored as a chain: each song knows only which song comes after it. Halfway through the drive, everyone agrees the last song should play first. Rewire the chain so it runs backwards, and hand back the new first song. The playlist holds at most 12 songs, and it might be empty.

What to return

Constraints

Example

Input
head = 4→8→15→16→23
Output
23→16→15→8→4

Solve it

More in Linked List

All algorithm problems