601. Reverse a Doubly Linked List

You are given the head of a doubly linked list.

Reverse the list in-place and return the new head of the reversed list.

Example 1:

Input: head = [10, 20, 30]

Output: [30, 20, 10]

Example 2:

Input: head = [1, 3, 5, 7, 9]

Output: [9, 7, 5, 3, 1]

Now Your Turn!

Pick the correct output for the given input

Input: head = [42]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= number of nodes <= 106
  • 0 <= node.val <= 104

0
/*
class ListNode {
public:
int data;
ListNode* prev;
ListNode* next;
 
ListNode(int val) : data(val), prev(nullptr), next(nullptr) {}
};
*/
 
class Solution {
public:
ListNode* reverseDLL(ListNode* head) {
// Your code goes here
}
};
Test Case

Input:

Head