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⁹
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Linked List
- Add Two NumbersAI round · Medium
- Merge Two Sorted ListsAI round · Easy
- Swap Nodes in PairsAI round · Medium
- Odd Even Linked ListAI round · Medium
- Convert Binary Search Tree to Sorted Doubly Linked ListMedium