Reverse a singly linked list and return its new head.
A singly linked list is a chain of nodes. Each ListNode holds a val and a next reference to the following node. The last node's next is null.
You get the head of the list. The head is the first node. Reuse the existing nodes by changing their next references. Don't build new nodes.
headcan benull(an empty list). In that case, returnnull.- The list can hold up to 100,000 nodes.
Input: head = 1 → 2 → 3 → 4
Output: 4 → 3 → 2 → 1
Input: head = 7
Output: 7
Input: head = null
Output: null