You are viewing a single comment's thread. Return to all comments →
highly appreciable, if someone minimize my if-else statements.
static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) { SinglyLinkedList sortedList = new SinglyLinkedList(); int flag=0; SinglyLinkedListNode l1=head1,l2=head2; while(flag!=1){ if(l1==null){ sortedList.insertNode(l2.data); l2=l2.next; }else if(l2==null){ sortedList.insertNode(l1.data); l1=l1.next; }else{ if(l1.data<l2.data){ sortedList.insertNode(l1.data); l1=l1.next; }else{ sortedList.insertNode(l2.data); l2=l2.next; } } if(l1==null && l2==null){ flag=1; } } return sortedList.head; }
Seems like cookies are disabled on this browser, please enable them to open this website
Merge two sorted linked lists
You are viewing a single comment's thread. Return to all comments →
highly appreciable, if someone minimize my if-else statements.