-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
56 lines (43 loc) · 890 Bytes
/
LinkedList.java
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
/******************************************************************************
Implementation of linked list to print elements..!
Input:
4
1 2 3 4
Output:
1 2 3 4
*******************************************************************************/
import java.util.*;
public class Main
{
static class Linked{
int val;
Linked next;
Linked(int d){
val=d;
next=null;
}
};
public static void print(Linked a){
while(a!=null){
System.out.print(a.val+" ");
a=a.next;
}
}
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int n=x.nextInt(),k;
Linked head=null,pre=null;
for(int i=0;i<n;i++){
k=x.nextInt();
Linked a=new Linked(k);
if(i==0){
head=a;
pre=a;
}else{
pre.next=a;
pre=a;
}
}
print(head);
}
}