Problems/206. Reverse Linked List

206. Reverse Linked List

EasyLinked ListRecursion

You are given the start node of a singly linked list. Rewrite the connections between the nodes so that the order of the list is completely reversed, then return the new starting node.

Example 1
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Reversing the connections turns 1 -> 2 -> 3 -> 4 -> 5 into 5 -> 4 -> 3 -> 2 -> 1.
Example 2
Input: head = [1, 2]
Output: [2, 1]
A list of two nodes reversed results in the second node becoming the head.
Example 3
Input: head = []
Output: []
An empty list has no nodes to reverse, so it remains empty.
Visualizer

Visualizer will appear here