3. Reverse a LL
Given the head of a singly linked list. Reverse the given linked list and return the head of the modified list.
Example 1:
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: The linked list is reversed by changing the direction of each link between consecutive nodes, resulting in the list [5, 4, 3, 2, 1].
Example 2:
Input: head = [6, 8]
Output: [8, 6]
Explanation: The link between the two nodes is reversed, changing the list from 6->8 to 8->6.
Now Your Turn!
Pick the correct output for the given inputInput: head = [1]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 0 <= number of nodes in the Linked List <= 105
- 0 <= ListNode.val <= 104