-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSinglyLinkedList.java
60 lines (52 loc) · 1.12 KB
/
SinglyLinkedList.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
57
58
59
60
package SummerTrainingGFG.LinkedList;
/**
* @author Vishal Singh
*/
class Node{
int data;
Node next;
Node(int data){
this.data = data;
}
}
class List{
Node head;
void insertBegin(int data){
Node temp = new Node(data);
temp.next = head;
head = temp;
}
void printList(){
Node curr = head;
while (curr != null){
System.out.print(curr.data+" ");
curr = curr.next;
}
System.out.println("");
}
void insertEnd(int data){
Node temp = new Node(data);
Node curr = head;
if (head == null){
head = temp;
return;
}
while (curr.next != null){
curr=curr.next;
}
curr.next = temp;
}
}
public class SinglyLinkedList {
public static void main(String[] args) {
List list = new List();
list.insertBegin(5);
list.insertBegin(15);
list.insertBegin(3);
list.printList();
list.insertEnd(100);
list.insertEnd(105);
list.insertEnd(1);
list.printList();
}
}