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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code