@@ -18,15 +18,15 @@ public class SinglyLinkedList {
18
18
private Node head ;
19
19
20
20
/**
21
- * size of SinglyLinkedList
21
+ * Size of SinglyLinkedList
22
22
*/
23
23
private int size ;
24
24
25
25
/**
26
- * init SinglyLinkedList
26
+ * Init SinglyLinkedList
27
27
*/
28
28
public SinglyLinkedList () {
29
- head = new Node ( 0 ) ;
29
+ head = null ;
30
30
size = 0 ;
31
31
}
32
32
@@ -42,87 +42,86 @@ public SinglyLinkedList(Node head, int size) {
42
42
}
43
43
44
44
/**
45
- * This method inserts an element at the head
45
+ * Inserts an element at the head of the list
46
46
*
47
- * @param x Element to be added
47
+ * @param x element to be added
48
48
*/
49
49
public void insertHead (int x ) {
50
50
insertNth (x , 0 );
51
51
}
52
52
53
53
/**
54
- * insert an element at the tail of list
54
+ * Insert an element at the tail of the list
55
55
*
56
- * @param data Element to be added
56
+ * @param data element to be added
57
57
*/
58
58
public void insert (int data ) {
59
59
insertNth (data , size );
60
60
}
61
61
62
62
/**
63
- * Inserts a new node at a specified position
63
+ * Inserts a new node at a specified position of the list
64
64
*
65
65
* @param data data to be stored in a new node
66
66
* @param position position at which a new node is to be inserted
67
67
*/
68
68
public void insertNth (int data , int position ) {
69
69
checkBounds (position , 0 , size );
70
- Node cur = head ;
71
- for (int i = 0 ; i < position ; ++i ) {
72
- cur = cur .next ;
70
+ Node newNode = new Node (data );
71
+ if (head == null ) { /* the list is empty */
72
+ head = newNode ;
73
+ size ++;
74
+ return ;
75
+ } else if (position == 0 ) { /* insert at the head of the list */
76
+ newNode .next = head ;
77
+ head = newNode ;
78
+ size ++;
79
+ return ;
73
80
}
74
- Node node = new Node (data );
75
- node .next = cur .next ;
76
- cur .next = node ;
77
- size ++;
78
- }
79
-
80
- /**
81
- * Insert element to list, always sorted
82
- *
83
- * @param data to be inserted
84
- */
85
- public void insertSorted (int data ) {
86
81
Node cur = head ;
87
- while ( cur . next != null && data > cur . next . value ) {
82
+ for ( int i = 0 ; i < position - 1 ; ++ i ) {
88
83
cur = cur .next ;
89
84
}
90
-
91
- Node newNode = new Node (data );
92
85
newNode .next = cur .next ;
93
86
cur .next = newNode ;
94
87
size ++;
95
88
}
96
89
90
+
97
91
/**
98
- * This method deletes an element at the head
99
- *
100
- * @return The element deleted
92
+ * Deletes a node at the head
101
93
*/
102
94
public void deleteHead () {
103
95
deleteNth (0 );
104
96
}
105
97
106
98
/**
107
- * This method deletes an element at the tail
99
+ * Deletes an element at the tail
108
100
*/
109
101
public void delete () {
110
102
deleteNth (size - 1 );
111
103
}
112
104
113
105
/**
114
- * This method deletes an element at Nth position
106
+ * Deletes an element at Nth position
115
107
*/
116
108
public void deleteNth (int position ) {
117
109
checkBounds (position , 0 , size - 1 );
110
+ if (position == 0 ) {
111
+ Node destroy = head ;
112
+ head = head .next ;
113
+ destroy = null ; /* clear to let GC do its work */
114
+ size --;
115
+ return ;
116
+ }
118
117
Node cur = head ;
119
- for (int i = 0 ; i < position ; ++i ) {
118
+ for (int i = 0 ; i < position - 1 ; ++i ) {
120
119
cur = cur .next ;
121
120
}
122
121
123
- // Node destroy = cur.next;
122
+ Node destroy = cur .next ;
124
123
cur .next = cur .next .next ;
125
- // destroy = null; // clear to let GC do its work
124
+ destroy = null ; // clear to let GC do its work
126
125
127
126
size --;
128
127
}
@@ -140,127 +139,112 @@ public void checkBounds(int position, int low, int high) {
140
139
}
141
140
142
141
/**
143
- * clear all nodes in list
142
+ * Clear all nodes in the list
144
143
*/
145
144
public void clear () {
146
- if (size == 0 ) {
147
- return ;
148
- }
149
- Node prev = head .next ;
150
- Node cur = prev .next ;
145
+ Node cur = head ;
151
146
while (cur != null ) {
152
- prev = null ; // clear to let GC do its work
153
- prev = cur ;
147
+ Node prev = cur ;
154
148
cur = cur .next ;
149
+ prev = null ; // clear to let GC do its work
155
150
}
156
- prev = null ;
157
- head .next = null ;
151
+ head = null ;
158
152
size = 0 ;
159
153
}
160
154
161
155
/**
162
156
* Checks if the list is empty
163
157
*
164
- * @return true is list is empty
158
+ * @return {@code true} if list is empty, otherwise {@code false}.
165
159
*/
166
160
public boolean isEmpty () {
167
161
return size == 0 ;
168
162
}
169
163
170
164
/**
171
- * Returns the size of the linked list
165
+ * Returns the size of the linked list.
166
+ *
167
+ * @return the size of the list.
172
168
*/
173
169
public int size () {
174
170
return size ;
175
171
}
176
172
173
+ /**
174
+ * Get head of the list.
175
+ *
176
+ * @return head of the list.
177
+ */
178
+ public Node getHead () {
179
+ return head ;
180
+ }
181
+
182
+ /**
183
+ * Calculate the count of the list manually
184
+ *
185
+ * @return count of the list
186
+ */
187
+ public int count () {
188
+ int count = 0 ;
189
+ Node cur = head ;
190
+ while (cur != null ) {
191
+ cur = cur .next ;
192
+ count ++;
193
+ }
194
+ return count ;
195
+ }
196
+
177
197
@ Override
178
198
public String toString () {
179
199
if (size == 0 ) {
180
200
return "" ;
181
201
}
182
202
StringBuilder builder = new StringBuilder ();
183
- Node cur = head . next ;
203
+ Node cur = head ;
184
204
while (cur != null ) {
185
205
builder .append (cur .value ).append ("->" );
186
206
cur = cur .next ;
187
207
}
188
208
return builder .replace (builder .length () - 2 , builder .length (), "" ).toString ();
189
209
}
190
210
191
- /**
192
- * Merge two sorted SingleLinkedList
193
- *
194
- * @param listA the first sorted list
195
- * @param listB the second sored list
196
- * @return merged sorted list
197
- */
198
- public static SinglyLinkedList merge (SinglyLinkedList listA , SinglyLinkedList listB ) {
199
- Node headA = listA .head .next ;
200
- Node headB = listB .head .next ;
201
-
202
- int size = listA .size () + listB .size ();
203
-
204
- Node head = new Node ();
205
- Node tail = head ;
206
- while (headA != null && headB != null ) {
207
- if (headA .value <= headB .value ) {
208
- tail .next = headA ;
209
- headA = headA .next ;
210
- } else {
211
- tail .next = headB ;
212
- headB = headB .next ;
213
- }
214
- tail = tail .next ;
215
- }
216
- if (headA == null ) {
217
- tail .next = headB ;
218
- }
219
- if (headB == null ) {
220
- tail .next = headA ;
221
- }
222
- return new SinglyLinkedList (head , size );
223
- }
224
211
225
212
/**
226
- * Main method
227
- *
228
- * @param args Command line arguments
213
+ * Driver Code
229
214
*/
230
- public static void main (String args []) {
231
- SinglyLinkedList myList = new SinglyLinkedList ();
232
- assert myList .isEmpty ();
233
- assert myList .toString ().equals ("" );
234
-
235
- myList .insertHead (5 );
236
- myList .insertHead (7 );
237
- myList .insertHead (10 );
238
- assert myList .toString ().equals ("10->7->5" );
239
-
240
- myList .deleteHead ();
241
- assert myList .toString ().equals ("7->5" );
242
-
243
- myList .insertNth (11 , 2 );
244
- assert myList .toString ().equals ("7->5->11" );
245
-
246
- myList .deleteNth (1 );
247
- assert myList .toString ().equals ("7->11" );
248
-
249
- myList .clear ();
250
- assert myList .isEmpty ();
251
-
252
- /* Test MergeTwoSortedLinkedList */
253
- SinglyLinkedList listA = new SinglyLinkedList ();
254
- SinglyLinkedList listB = new SinglyLinkedList ();
255
-
256
- for (int i = 10 ; i >= 2 ; i -= 2 ) {
257
- listA .insertSorted (i );
258
- listB .insertSorted (i - 1 );
215
+ public static void main (String [] arg ) {
216
+ SinglyLinkedList list = new SinglyLinkedList ();
217
+ assert list .isEmpty ();
218
+ assert list .size () == 0
219
+ && list .count () == 0 ;
220
+ assert list .toString ().equals ("" );
221
+
222
+ /* Test insert function */
223
+ list .insertHead (5 );
224
+ list .insertHead (7 );
225
+ list .insertHead (10 );
226
+ list .insert (3 );
227
+ list .insertNth (1 , 4 );
228
+ assert list .toString ().equals ("10->7->5->3->1" );
229
+
230
+ /* Test delete function */
231
+ list .deleteHead ();
232
+ list .deleteNth (1 );
233
+ list .delete ();
234
+ assert list .toString ().equals ("7->3" );
235
+
236
+ assert list .size == 2
237
+ && list .size () == list .count ();
238
+
239
+ list .clear ();
240
+ assert list .isEmpty ();
241
+
242
+ try {
243
+ list .delete ();
244
+ assert false ; /* this should not happen */
245
+ } catch (Exception e ) {
246
+ assert true ; /* this should happen */
259
247
}
260
- assert listA .toString ().equals ("2->4->6->8->10" );
261
- assert listB .toString ().equals ("1->3->5->7->9" );
262
- assert SinglyLinkedList .merge (listA , listB ).toString ().equals ("1->2->3->4->5->6->7->8->9->10" );
263
-
264
248
}
265
249
}
266
250
0 commit comments