849. Convert Doubly Linked List to Array II

You are given the random node 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,4,3,2,6,5,7]

Output:  [1,4,3,2,6,5,7]

Example 2:

Input: head = [1,2,3,4,5]

Output: [1,2,3,4,5] 

Now Your Turn!

Pick the correct output for the given input

Input: head = [3,2,1,4,5,7]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • The number of nodes in the given list is in the range [1, 500].
  • 1 <= Node.val <= 1000
  • All nodes have unique Node.val
0
/**
* 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) {
}
};
 
Test Case

Input:

Head