Reverse a Linked List
Problem statement
Given the head of an acyclic singly linked list, reverse all of its next pointers and return the new head. Reuse the original data nodes. An empty list returns null.
Input and output lists are displayed as their node-value sequences. For this exercise, the report's brief reverse-list recollection is practiced as reversal of a singly linked list.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [1,2,3,4]return = [4,3,2,1]The original final node becomes the new head.
Example 2
head = []return = []Reversing an empty list still produces null.
Constraints
- The list contains between
0and10000nodes. -10^9 <= node.val <= 10^9.