-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
234 lines (164 loc) · 4.7 KB
/
LinkedList.java
File metadata and controls
234 lines (164 loc) · 4.7 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
class ListNode{
private int data;
private ListNode next;
public ListNode(int data){
this.data = data;
}
public void setData(int data){
this.data = data;
}
public int getData(){
return data;
}
public void setNext(ListNode next){
this.next = next;
}
public ListNode getNext(){
return this.next;
}
}
public class LinkedList{
int length;
public LinkedList(){
length = 0;
}
ListNode head;
//return head
public synchronized ListNode getHead(){
return head;
}
//insert at the beginning position
public synchronized void insertAtBegin(ListNode node){
//if head is null, we have no list
node.setNext(head);
head = node;
length++;
}
//insert at the last position
public synchronized void insertAtEnd(ListNode node){
if(head == null){
head = node;
}else{
ListNode p , q;
for(p = head ; (q = p.getNext()) != null ; p = q);
p.setNext(node);
}
length++;
}
//insert at random position
public synchronized void insert(int data , int position){
//to validate if position is valid or not
if(position < 0){
position = 0;
}
if(position > length){
position = length;
}
if(head == null){
head = new ListNode(data);
}
ListNode temp = head;
for(int i = 1 ; i < position ; i++){
temp = temp.getNext();
}
ListNode node = new ListNode(data);
node.setNext(temp.getNext());
temp.setNext(node);
}
//remove the first node in list
public synchronized ListNode removeFromBegin(){
ListNode node = head;
if(node != null){
head = node.getNext();
node.setNext(null);
length--;
}
return node;
}
//remove the last node in list
public synchronized ListNode removeFromLast(){
if(head == null){
return null;
}
ListNode p = head , q = null , next = head.getNext();
if(next == null){
head = null;
return p;
}
while((next = p.getNext()) != null){
q = p;
p = next;
}
q.setNext(null);
length -= 1;
return p;
}
// remove the matching node
public synchronized void removeMatched(ListNode node){
// if head is null, do nothing
if(head == null){
return;
}
//if head is not null, check if head itself the node;
if(node.equals(head)){
head = head.getNext();
return;
}
//else check if next or any of the node after head is the node
ListNode p = head , q = null ;
while((q = p.getNext()) != null){
if(node.equals(q)){ // we meet the node here
p.setNext(q.getNext()); // setting its previous node to point to matched node's node
length -= 1;
return;
}
p = q; // to move ahead in the list , if the current nod is not the node
}
}
// remove at any position
public void remove(int position){
if(position < 0){
position = 0;
}
if(position > length){
position = length - 1;
}
if(position == 0){
head = head.getNext();
return;
}
else{
ListNode p = head;
//loop till we reach a position one before the acutal element
for(int i = 1 ; i < position ; i++){
p = p.getNext();
}
p.setNext(p.getNext().getNext());
}
length -= 1;
}
public String toString(){
String result = "[";
if(head == null){
result += "]";
}
ListNode temp = head;
while(temp != null){
result += temp.getData();
if(temp.getNext() != null){
result += " , ";
}
temp = temp.getNext();
}
result += "]";
return result;
}
public static void main(String[] args){
LinkedList ll = new LinkedList();
ListNode node = new ListNode(10);
ll.insertAtBegin(node);
System.out.println(ll.toString());
ll.insert(20, 1);
System.out.println(ll.toString());
}
}