Convert Doubly Linked List to Array II - Problem

You are given an arbitrary node from a doubly linked list, which contains nodes that have a next pointer and a previous pointer. Return an integer array which contains the elements of the linked list in order.

Since you start from an arbitrary node (not necessarily the head), you need to first find the beginning of the list, then traverse from head to tail to collect all values.

Input & Output

Example 1 — Starting from Middle
$ Input: node pointing to value 3 in list [1,2,3,4,5]
Output: [1,2,3,4,5]
💡 Note: Starting from node with value 3, we move backward to find head (value 1), then traverse forward collecting all values: 1→2→3→4→5
Example 2 — Starting from Head
$ Input: node pointing to value 10 in list [10,20,30]
Output: [10,20,30]
💡 Note: Starting from head node (value 10), no backward movement needed, just traverse forward: 10→20→30
Example 3 — Single Node
$ Input: node pointing to value 42 in list [42]
Output: [42]
💡 Note: List has only one node, so it's both head and tail. Result is simply [42]

Constraints

  • The number of nodes in the list is in the range [1, 104]
  • -105 ≤ Node.val ≤ 105
  • The given node is guaranteed to be part of a doubly linked list

Visualization

Tap to expand
Convert Doubly Linked List to Array IIGiven arbitrary node → Return complete ordered array12345Given Node (arbitrary)1. Find head by going backward2. Traverse forward collecting valuesResult: [1, 2, 3, 4, 5]
Understanding the Visualization
1
Input
Given arbitrary node in doubly linked list [1,2,3,4,5]
2
Process
Find head by going backward, then traverse forward
3
Output
Return complete array [1,2,3,4,5]
Key Takeaway
🎯 Key Insight: First find the beginning, then collect everything in order
Asked in
Microsoft 25 Amazon 20 Google 15
28.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen