Skip to main content
Back to problems
#3263
Easy Algorithms

Convert doubly linked list to array i

Array Linked List Doubly-Linked List
94.9% acceptance
Mar 31, 2026
21
6
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.

Solution

C++
Time O(n)
Space O(1)
LeetCode
solution.cpp
/**
 * Definition for doubly-linked list.
 * class Node {
 *     int val;
 *     Node* prev;
 *     Node* next;
 *     Node() : val(0), next(nullptr), prev(nullptr) {}
 *     Node(int x) : val(x), next(nullptr), prev(nullptr) {}
 *     Node(int x, Node *prev, Node *next) : val(x), next(next), prev(prev) {}
 * };
 */
class Solution {
public:
  vector<int> toArray(Node* head) {
    vector<int> result;
    Node* cur = head;
    while (cur) {
      result.push_back(cur->val);
      cur = cur->next;
    }
    return result;
  }
};