We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Essentially the same thing but little clean and easy to understand
/* class Node { int data; Node next; }*/NodemergeLists(NodeheadA,NodeheadB){Nodea=headA;Nodeb=headB;Nodec=null;if(a==null||b==null)// either of them is nullreturna==null?b:a;// getting the first index pointed by c, done this so now in the coming loop i can use next directly on cif(a.data<b.data){c=a;a=a.next;}else{c=b;b=b.next;}Nodehead=c;// either one of a or b has been changed,while(a!=null&&b!=null){// ie going to the last elements of the linked listsif(a.data<b.data){c.next=a;a=a.next;}else{c.next=b;b=b.next;}c=c.next;}// either one of a and b is finished// a is leftif(a!=null)c.next=a;// b is leftif(b!=null)c.next=b;returnhead;}
Merge two sorted linked lists
You are viewing a single comment's thread. Return to all comments →
Essentially the same thing but little clean and easy to understand