@@ -30,6 +30,17 @@ public SinglyLinkedList() {
3030 size = 0 ;
3131 }
3232
33+ /**
34+ * Init SinglyLinkedList with specified head node and size
35+ *
36+ * @param head the head node of list
37+ * @param size the size of list
38+ */
39+ public SinglyLinkedList (Node head , int size ) {
40+ this .head = head ;
41+ this .size = size ;
42+ }
43+
3344 /**
3445 * This method inserts an element at the head
3546 *
@@ -66,6 +77,23 @@ public void insertNth(int data, int position) {
6677 size ++;
6778 }
6879
80+ /**
81+ * Insert element to list, always sorted
82+ *
83+ * @param data to be inserted
84+ */
85+ public void insertSorted (int data ) {
86+ Node cur = head ;
87+ while (cur .next != null && data > cur .next .value ) {
88+ cur = cur .next ;
89+ }
90+
91+ Node newNode = new Node (data );
92+ newNode .next = cur .next ;
93+ cur .next = newNode ;
94+ size ++;
95+ }
96+
6997 /**
7098 * This method deletes an element at the head
7199 *
@@ -142,7 +170,7 @@ public boolean isEmpty() {
142170 /**
143171 * Returns the size of the linked list
144172 */
145- public int getSize () {
173+ public int size () {
146174 return size ;
147175 }
148176
@@ -160,38 +188,78 @@ public String toString() {
160188 return builder .replace (builder .length () - 2 , builder .length (), "" ).toString ();
161189 }
162190
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+
163225 /**
164226 * Main method
165227 *
166228 * @param args Command line arguments
167229 */
168230 public static void main (String args []) {
169231 SinglyLinkedList myList = new SinglyLinkedList ();
170-
171232 assert myList .isEmpty ();
233+ assert myList .toString ().equals ("" );
172234
173235 myList .insertHead (5 );
174236 myList .insertHead (7 );
175237 myList .insertHead (10 );
176-
177- System .out .println (myList );; // 10 -> 7 -> 5
238+ assert myList .toString ().equals ("10->7->5" );
178239
179240 myList .deleteHead ();
180-
181- System .out .println (myList );; // 7 -> 5
241+ assert myList .toString ().equals ("7->5" );
182242
183243 myList .insertNth (11 , 2 );
184-
185- System .out .println (myList );; // 7 -> 5 -> 11
244+ assert myList .toString ().equals ("7->5->11" );
186245
187246 myList .deleteNth (1 );
188-
189- System .out .println (myList );; // 7-> 11
247+ assert myList .toString ().equals ("7->11" );
190248
191249 myList .clear ();
192250 assert myList .isEmpty ();
193251
194- System .out .println (myList ); // ""
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 );
259+ }
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" );
195263
196264 }
197265}
@@ -212,13 +280,21 @@ class Node {
212280 */
213281 Node next ;
214282
283+ Node () {
284+
285+ }
286+
215287 /**
216288 * Constructor
217289 *
218290 * @param value Value to be put in the node
219291 */
220292 Node (int value ) {
293+ this (value , null );
294+ }
295+
296+ Node (int value , Node next ) {
221297 this .value = value ;
222- this .next = null ;
298+ this .next = next ;
223299 }
224300}
0 commit comments