[LeetCode] Convert Doubly Linked List to Array I

3263. Convert Doubly Linked List to Array I

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 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> res;
while(head) {
res.push_back(head->val);
head = head->next;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/08/31/PS/LeetCode/convert-doubly-linked-list-to-array-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.