Reverse Linked List

Very easy Linked List AI round

head is the head of a singly linked list, which may be empty.

Reverse the list: the last node comes first, and each node ends up pointing at the node that used to come before it. The old head, now last, points at nothing.

Return the head of the reversed list, or null if the list is empty.

Example 1

Input: head = [4,8,15,16,23]
Output: [23,16,15,8,4]

The last node, 23, becomes the head, and every node now points at the one that used to be before it. 4, the old head, ends the list.

Example 2

Input: head = [9,9,2]
Output: [2,9,9]

Example 3

Input: head = []
Output: []

Constraints

  • 0 ≤ number of nodes ≤ 10⁵
  • -10⁹ ≤ node.val ≤ 10⁹

Watch the explanation

More Linked List

All Linked List problems