Convert Doubly Linked List to Array I - Solution & Explanation
Problem Statement
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.
Example 1:
Input: head = [1,2,3,4,3,2,1]
Output: [1,2,3,4,3,2,1]
Example 2:
Input: head = [2,2,2,2,2]
Output: [2,2,2,2,2]
Example 3:
Input: head = [3,2,3,2,3,2]
Output: [3,2,3,2,3,2]
Constraints:
- The number of nodes in the given list is in the range
[1, 50]. 1 <= Node.val <= 50
Approach Overview
Problem Overview: Given the head of a doubly linked list, the task is to return an array containing the node values in the same order as they appear in the list. You start from the head node and follow the next pointers until the end, copying each value into an array.
Approach 1: Direct Traversal (O(n) time, O(n) space)
The most straightforward solution is to iterate through the doubly linked list starting from the head. At each node, append the node's value to a dynamic array (such as a Python list or Java ArrayList). Continue moving through the list using the next pointer until you reach null. This works because a doubly linked list preserves element order through its sequential node connections. The traversal touches every node exactly once, giving O(n) time complexity where n is the number of nodes. The array stores all values, so the extra space used is O(n). This approach is simple, readable, and the most common implementation in interviews.
Approach 2: Two-Pass Traversal with Preallocated Array (O(n) time, O(n) space)
Another option is to first walk through the list to count the number of nodes. Once you know the size, allocate an array of that exact length. Then perform a second traversal and fill the array sequentially with node values. This avoids dynamic resizing that some array implementations perform during repeated appends. The trade-off is an additional pass through the linked list. Time complexity remains O(n) since each node is visited twice, and space complexity stays O(n) for the resulting array.
Recommended for interviews: Direct traversal is the expected answer. Interviewers want to see that you understand how to iterate through a linked structure using pointers and convert it into a contiguous array representation. Mentioning the two-pass preallocation variant shows deeper awareness of memory behavior in dynamic arrays, but the single-pass append approach is typically preferred for its clarity and minimal code.
Solution
We can directly traverse the linked list, adding the values of the nodes to the answer array ans one by one.
After the traversal is complete, return the answer array ans.
The time complexity is O(n), where n is the length of the linked list. Ignoring the space consumption of the answer array, the space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Traversal | O(n) | O(n) | Best general solution. Simple single pass through the list while appending values to an array. |
| Two-Pass Traversal with Preallocation | O(n) | O(n) | Useful when you want to allocate the exact array size before filling it. |
Video Solution
leetcode 3263: convert doubly linked list to array : python solution • leetcode blind 75 • 301 views views
Watch 2 more video solutions →Frequently Asked Questions
Is Convert Doubly Linked List to Array I easy or hard?
Convert Doubly Linked List to Array I Python/Java solution
How to solve Convert Doubly Linked List to Array I in O(n)?
What is the best approach for Convert Doubly Linked List to Array I?
Is Convert Doubly Linked List to Array I asked at Google/Amazon/Meta?
What data structure is used in Convert Doubly Linked List to Array I?
What is the time complexity of Convert Doubly Linked List to Array I?
Ready to solve this problem?
Practice Convert Doubly Linked List to Array I with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor