-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay15_LinkedList.java
More file actions
executable file
·59 lines (50 loc) · 1.14 KB
/
Copy pathDay15_LinkedList.java
File metadata and controls
executable file
·59 lines (50 loc) · 1.14 KB
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
/*
* Sapayth Hossain
*/
package ThirtyDaysOfCode;
import java.util.Scanner;
/*
* @author sapaythhossain
*/
public class Day15_LinkedList {
public static Node1 insert(Node1 head, int data) {
//Complete this method
if (head == null) {
head = new Node1(data);
} else {
Node1 curr = head;
while (curr.next != null) {
curr = curr.next;
}
Node1 temp = new Node1(data);
curr.next = temp;
}
return head;
}
public static void display(Node1 head) {
Node1 start = head;
while (start != null) {
System.out.print(start.data + " ");
start = start.next;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Node1 head = null;
int N = sc.nextInt();
while (N-- > 0) {
int ele = sc.nextInt();
head = insert(head, ele);
}
display(head);
sc.close();
}
}
class Node1 {
int data;
Node1 next;
Node1(int d) {
data = d;
next = null;
}
}