You are viewing a single comment's thread. Return to all comments →
I could not come up with an elegant solution as you did. Here is mine:
Node MergeLists(Node headA, Node headB) { Node merge; Node head; if(headA == null){ return headB; }else if(headB == null){ return headA; }else{ if(headA.data < headB.data){ merge = headA; headA = headA.next; }else{ merge = headB; headB = headB.next; } head = merge; } while(headA != null || headB != null){ if(headA == null){ merge.next = headB; break; }else if(headB == null){ merge.next = headA; break; }else{ if(headA.data < headB.data){ merge.next = headA; headA = headA.next; }else{ merge.next = headB; headB = headB.next; } merge = merge.next; } } 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 →
I could not come up with an elegant solution as you did. Here is mine: