Skip to main content

Convert Doubly Linked List to Array I - Solution & Explanation

EasyPremiumFree on FleetCodeArrayLinked ListDoubly-Linked List5 min read
Practice this problem

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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct TraversalO(n)O(n)Best general solution. Simple single pass through the list while appending values to an array.
Two-Pass Traversal with PreallocationO(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 is considered an Easy problem. It mainly tests basic understanding of linked list traversal and how to transfer node values into a sequential array structure.
Convert Doubly Linked List to Array I Python/Java solution
The Python solution typically uses a list and appends values while traversing the nodes. In Java, an ArrayList is commonly used during traversal and then returned as an array if required. Both implementations follow the same O(n) traversal logic.
How to solve Convert Doubly Linked List to Array I in O(n)?
Traverse the list starting from the head and follow the next pointers until you reach the end. For every node visited, push its value into an array or list. Since each node is processed once, the algorithm runs in O(n) time with O(n) additional space.
What is the best approach for Convert Doubly Linked List to Array I?
Direct traversal is the best approach. Start from the head node and iterate through the doubly linked list using the next pointer, appending each node value to an array. This processes every node exactly once, resulting in O(n) time and O(n) space.
Is Convert Doubly Linked List to Array I asked at Google/Amazon/Meta?
Problems involving linked list traversal and conversions appear frequently in interviews at companies like Amazon, Google, and Meta. While this exact problem may vary, the underlying concept of iterating through linked list nodes and building another structure is a common interview pattern.
What data structure is used in Convert Doubly Linked List to Array I?
The problem involves two core data structures: a doubly linked list for the input and an array for the output. The algorithm simply walks through the linked list and copies each node value into the array in order.
What is the time complexity of Convert Doubly Linked List to Array I?
The optimal solution runs in O(n) time because each node in the doubly linked list is visited exactly once during traversal. The resulting array stores all node values, so the space complexity is O(n).

Ready to solve this problem?

Practice Convert Doubly Linked List to Array I with our built-in code editor and test cases.

Practice on FleetCode