Convert Doubly Linked List to Array I - Problem

You are given the head of a doubly linked list, where each node contains an integer value and has both next and prev pointers. Your task is to traverse this doubly linked list and convert it into a regular integer array.

A doubly linked list is a data structure where each node points to both the next node and the previous node in the sequence. This allows for efficient bidirectional traversal, but for this problem, you'll only need to traverse in one direction.

Goal: Return an integer array containing all the values from the doubly linked list in the same order they appear when traversing from head to tail.

Example:
If your doubly linked list looks like: 1 ⇄ 2 ⇄ 3 ⇄ null
Then your output array should be: [1, 2, 3]

Input & Output

example_1.py — Basic Case
$ Input: head = [1,2,3,4,5]
Output: [1,2,3,4,5]
💡 Note: The doubly linked list 1⇄2⇄3⇄4⇄5 is converted to array [1,2,3,4,5] by traversing from head to tail.
example_2.py — Single Node
$ Input: head = [42]
Output: [42]
💡 Note: A doubly linked list with only one node containing value 42 becomes an array with one element [42].
example_3.py — Empty List
$ Input: head = []
Output: []
💡 Note: An empty doubly linked list (head = null) results in an empty array [].

Constraints

  • The number of nodes in the list is in the range [0, 1000]
  • Node values are in the range [-1000, 1000]
  • Follow up: Can you solve it using only one pass through the linked list?

Visualization

Tap to expand
Doubly Linked List to Array Conversionprev1nextprev2nextprev3nextHEAD123[0][1][2]RESULT ARRAYAlgorithm: Traverse linked list from HEAD → collect values → build arraycurrent
Understanding the Visualization
1
Start at Head
Position yourself at the first node of the doubly linked list
2
Collect Value
Add the current node's value to your result array
3
Move Forward
Follow the 'next' pointer to advance to the next node
4
Repeat Until End
Continue steps 2-3 until you reach a null pointer
5
Return Result
Your array now contains all values in the original order
Key Takeaway
🎯 Key Insight: Converting a doubly linked list to an array is simply a matter of forward traversal - we don't need the backward pointers, just follow the 'next' pointers and collect values in order.
Asked in
Amazon 45 Microsoft 32 Google 28 Meta 15
23.4K Views
Medium Frequency
~8 min Avg. Time
892 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