Given the head of a singly linked list in C++, reverse the list and return the new head pointer. You must do this in-place by manipulating raw pointers.
Examples
Example 1 Input:head = [1, 2, 3]
Output:[3, 2, 1]
Constraints
▪Number of nodes in range [0, 5000]
▪-5000 <= Node.val <= 5000
Hints
Hint 1Show
Keep track of `prev`, `curr`, and `next` pointers.
Hint 2Show
Change `curr->next` pointer to point to `prev` on each step.
Starter Code
Solution.cpp
1/**
2 * struct ListNode {
3 * int val;
4 * ListNode *next;
5 * };
6 */
7ListNode* reverseList(ListNode* head) {
8 // Your code here
9 return nullptr;
10}
Solution
Solve the problem first before reviewing the solution!