You are viewing a single comment's thread. Return to all comments →
Simple non recursive solution:
Node MergeLists(Node headA, Node headB) { if(headA==null || headB == null) return headA==null?headB:headA; Node list=null; if(headA.data < headB.data){ list = headA; } else { list = headB; headB= headA; headA=list; } while(headA.next!=null){ if(headA.next.data > headB.data){ Node next = headA.next; headA.next=headB; headB= next; } headA= headA.next; } if(headA.next==null) headA.next = headB; return list; }
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 →
Simple non recursive solution: