-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked list
60 lines (48 loc) · 1.2 KB
/
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
58
59
60
//Linked list
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
class Node {
int data;
Node next;
Node() { data = 0; }
Node(int d) { data = d; }
}
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 a[] = new int[n];
for(int i = 0; i < n; i++)
a[i] = sc.nextInt();
Solution obj = new Solution();
Node ans = obj.constructLL(a);
while(ans != null) {
System.out.print(ans.data + " ");
ans = ans.next;
}
System.out.println();
}
}
}
class Node {
int data;
Node next;
Node() { data = 0; }
Node(int d) { data = d; }
}
class Solution {
static Node constructLL(int arr[]) {
Node head = new Node(arr[0]);
Node curr = head;
for(int i = 1; i < arr.length; i++) {
Node newNode = new Node(arr[i]);
curr.next = newNode;
curr = curr.next;
}
return head;
}
}