Convert Doubly Linked List to Array I - Problem
You are given the head of 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.
Note: A doubly linked list allows traversal in both forward and backward directions.
Input & Output
Example 1 — Basic Case
$
Input:
head = [1,2,3,4]
›
Output:
[1,2,3,4]
💡 Note:
Traverse the doubly linked list from head: 1 → 2 → 3 → 4, collect each value into array
Example 2 — Single Node
$
Input:
head = [5]
›
Output:
[5]
💡 Note:
Only one node in the list, so the result array contains just that single value
Example 3 — Negative Values
$
Input:
head = [-1,0,1]
›
Output:
[-1,0,1]
💡 Note:
Doubly linked list with negative, zero, and positive values: -1 → 0 → 1
Constraints
- The number of nodes in the linked list is in the range [1, 1000]
- -1000 ≤ Node.val ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Doubly linked list with nodes containing values and prev/next pointers
2
Process
Traverse from head to tail, collecting node values
3
Output
Array containing all node values in original order
Key Takeaway
🎯 Key Insight: Simply traverse the doubly linked list once from head to tail, collecting each node's value into the result array
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code