Reverse a Singly Linked List
Problem statement
Given the head of a finite, acyclic singly linked list, reverse every next pointer and return the new head.
Reuse the original nodes and preserve every value. Return null for an empty list.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [1,2,3,4]return = [4,3,2,1]The original tail becomes the new head and every link points backward.
Example 2
head = []return = []An empty list remains empty.
Constraints
0 <= number of nodes <= 100000.-1000000000 <= node.val <= 1000000000.- The input list is finite and acyclic.