-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathSinglyLinkedList.java
159 lines (129 loc) · 3.4 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.Objects;
class Node {
int val;
Node next;
Node(int val) {
this.val = val;
this.next = null;
}
Node(int val, Node next) {
this.val = val;
this.next = next;
}
}
public class SinglyLinkedList {
Node head;
public void printList() {
Node aux = head;
System.out.print("[");
while (aux != null) {
System.out.print(aux.val);
if (aux.next != null) {
System.out.print(", ");
}
aux = aux.next;
}
System.out.println("]");
}
public void pushBack(int val) {
Node newNode = new Node(val);
if (head == null) {
head = newNode;
} else {
Node aux = head;
while (aux.next != null) {
aux = aux.next;
}
aux.next = newNode;
}
}
public void pushFront(int val) {
Node newNode = new Node(val, head);
head = newNode;
}
public void insertInPosition(int val, int pos) {
if (pos == 0) {
pushFront(val);
return;
}
Node newNode = new Node(val);
Node prev = head;
Node curr = head;
while (curr != null && pos > 0) {
prev = curr;
curr = curr.next;
pos--;
}
prev.next = newNode;
newNode.next = curr;
}
public void popBack() {
if (head == null) {
return;
}
if (head.next == null) {
head = null;
return;
}
Node prev = head;
Node curr = head.next;
while (curr.next != null) {
prev = prev.next;
curr = curr.next;
}
prev.next = null;
}
public void popFront() {
if (head == null) {
return;
}
Node aux = head;
head = aux.next;
}
public void removeFromPosition(int pos) {
if (head == null) {
return;
}
if (pos == 0) {
popFront();
return;
}
Node prev = head;
Node curr = head;
while (curr != null && pos > 0) {
prev = curr;
curr = curr.next;
pos--;
}
if (curr != null) {
prev.next = curr.next;
}
}
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
System.out.println("Inserting the elements at the end [1, 2, 3]:");
list.pushBack(1);
list.pushBack(2);
list.pushBack(3);
list.printList();
System.out.println("Inserting the elements at the beginning [0, -1, -2]:");
list.pushFront(0);
list.pushFront(-1);
list.pushFront(-2);
list.printList();
System.out.println("Inserting in positions 3, 4, 5 the elements [997, 998, 999] respectively:");
list.insertInPosition(997, 3);
list.insertInPosition(998, 4);
list.insertInPosition(999, 5);
list.printList();
System.out.println("Removing last element:");
list.popBack();
list.printList();
System.out.println("Removing first element:");
list.popFront();
list.printList();
System.out.println("Removing element in position 2:");
list.removeFromPosition(2);
list.printList();
}
}