Reverse a Linked List
Implement reverseList
Given the head of a singly linked list, flip the direction of every link so the list reads back to front, and return the new head — the node that used to be last.
Every node's link needs to point at whatever used to come before it instead of what used to come after. Walking the list once while carrying a "reversed so far" pointer, and re-pointing each node's link back at that pointer before sliding both pointers forward, flips every link in a single pass. The recursive version reaches the same result from the opposite direction: it dives all the way to the last node first, then fixes one link per call as the recursion unwinds back to the front.
Example 1:
Input: head = [3,6,2,9]
Output: [9,2,6,3]
Example 2:
Input: head = [7,1]
Output: [1,7]
Example 3:
Input: head = []
Output: []
+ 6 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes in head ≤ 200 - ●
-1000 ≤ node value ≤ 1000
head =
[3, 6, 2, 9]