Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You are given an array ** ‘Arr’** of size

Your task is to make a doubly linked list from the array and return the head of the linked list.

Here, the head of the doubly linked list is the first element of the array, and the tail of the doubly linked list is the last element.

```
A doubly linked list is one in which it is possible to access the next and the previous nodes from a node in the linked list (if they exist).
```

```
Input: ‘N’ = 4, ‘Arr’ = [4, 2, 5, 1]
Output: 4 2 5 1
Explanation: Doubly Linked List for the array ‘Arr’ = [4, 2, 5, 1] is 4 <-> 2 <-> 5 <-> 1.
```

Detailed explanation

```
The first line contains one integer, ‘N’, denoting the size of the array ‘Arr’.
The following line contains ‘N’ integers, denoting the ‘Arr’.
```

```
Return the head of the doubly linked list.
```

```
You don't need to print anything. Just implement the given function. Contents of your returned doubly linked list will be printed.
```

```
4
4 2 5 1
```

```
4 2 5 1
```

```
Input: ‘N’ = 4, ‘Arr’ = [4, 2, 5, 1]
Output: 4 2 5 1
Explanation:
Doubly Linked List for the array ‘Arr’ = [4, 2, 5, 1] is 4 <-> 2 <-> 5 <-> 1.
```

```
5
4 3 2 1 5
```

```
4 3 2 1 5
```

```
Input: ‘N’ = 5, ‘Arr’ = [4, 3, 2, 1, 5]
Output: 4 3 2 1 5
Explanation:
Doubly Linked List for the array ‘Arr’ = [4, 3, 2, 1, 5] is 4 <-> 3 <-> 2 <-> 1 <-> 5.
```

```
Try solving this in O(N).
```

```
1 <= 'N' <= 10^4
1 <= 'Arr[i]' <= 10^5
Time Limit: 1 sec
```