345. Reverse a Doubly Linked List

EasyLinked ListDoubly Linked ListDLLReversal

Given the head of a doubly linked list, reverse it in-place by swapping each node's next and prev pointers. Return the new head.

Input: Head of a doubly linked list.

Output: New head of the reversed DLL (values array).

Examples

Example 1
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]
Explanation: Swap next/prev of every node. Old tail becomes new head.
Example 2
Input: [1,2]
Output: [2,1]
Explanation: Swap: 2.prev=NULL; 1.next=NULL; 2.next=1; 1.prev=2.
Example 3
Input: [7]
Output: [7]
Explanation: Single node unchanged.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →