Reverse Linked List
Problem statement
Given the head of a singly linked list, reverse its node order by changing its next pointers and return the new head. Keep every node value unchanged.
The input is a finite acyclic list. Its displayed array lists node values from head to tail; [] represents a null head.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [4,1,7]return = [7,1,4]The node order 4 → 1 → 7 becomes 7 → 1 → 4. The new tail points to null, so the result is [7,1,4].
Example 2
head = []return = []There are no nodes to reverse. Return null, displayed as [].
Constraints
0 <= head.length <= 500-1000000 <= head[i] <= 1000000