-
Notifications
You must be signed in to change notification settings - Fork 0
/
Introduction to doubly linked list
57 lines (46 loc) · 1.18 KB
/
Introduction to doubly linked list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Introduction to doubly linked list
import java.io.*;
import java.util.*;
class Node {
int data;
Node next;
Node prev;
Node(int x) {
data = x;
next = null;
prev= null;
}
}
class GFG {
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int[] arr =new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
Solution obj = new Solution();
Node ans = obj.constructDLL(arr);
while(ans != null) {
System.out.print(ans.data + " ");
ans = ans.next;
}
System.out.println();
}
}
}
class Solution {
Node constructDLL(int arr[]) {
Node head = new Node(arr[0]);
Node temp = head;
for(int i = 1; i < arr.length; i++) {
Node newNode = new Node(arr[i]);
newNode.next = null;
newNode.prev = temp;
temp.next = newNode;
temp = newNode;
}
return head;
}
}