875. Convert Array to Doubly Linked List

Given an array arr, convert it into a doubly linked list and return the head of the list.

Example 1:

Input: arr = [1, 2, 3, 4]

Output: [1, 2, 3, 4]

Example 2:

Input: arr = [7, 7]

Output: [7, 7]

Now Your Turn!

Pick the correct output for the given input

Input: nums = [3]

Still unsure what the problem is asking ?

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

Constraints:

  • n == nums.length
  • 0 <= n <= 100
  • 0 <= nums[i] <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
class ListNode
{
* Definition for doubly-linked list.
* public:
* int data;
* ListNode *prev;
* ListNode *next;
* ListNode() : data(0), prev(nullptr), next(nullptr) {}
* ListNode(int x) : data(x), prev(nullptr), next(nullptr) {}
* ListNode(int x, ListNode *prev, ListNode *next) : data(x), prev(prev), next(next) {}
};
*/
 
class Solution
{
public:
ListNode *arrayToDoublyLinkedList(vector<int> &arr) {
// Your code goes here
}
};
Test Case

Input:

Nums