334. Doubly Linked List — Reverse Traversal

EasyLinked ListDoubly Linked ListDLLTraversalReversal

Given the head of a doubly linked list, collect all node values by traversing from head to tail, then return an array of values in reverse order (tail to head) without modifying the list.

First reach the tail using next pointers, then traverse back using prev pointers.

Input: Head of a doubly linked list.

Output: Array of node values from tail to head.

Examples

Example 1
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]
Explanation: Traverse next to tail(5). Traverse prev: 5→4→3→2→1. Collect [5,4,3,2,1].
Example 2
Input: [10,20,30]
Output: [30,20,10]
Explanation: Tail=30. Reverse: [30,20,10].
Example 3
Input: [7]
Output: [7]
Explanation: Single node. Tail=head. Return [7].

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →