You are viewing a single comment's thread. Return to all comments →
static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) { SinglyLinkedListNode newList = null; if(head1.data < head2.data){ newList = new SinglyLinkedListNode(head1.data); head1 = head1.next; }else{ newList = new SinglyLinkedListNode(head2.data); head2 = head2.next; } var head = newList; while(head1 != null && head2 != null){ if(head1.data < head2.data){ newList.next = new SinglyLinkedListNode(head1.data); head1 = head1.next; }else{ newList.next = new SinglyLinkedListNode(head2.data); head2 = head2.next; } newList = newList.next; } newList.next = (head1 != null) ? head1 : head2; return 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 →