You are viewing a single comment's thread. Return to all comments →
java code
SinglyLinkedListNode pt1=head1,pt2=head2; SinglyLinkedListNode cur=new SinglyLinkedListNode(0); SinglyLinkedListNode head3=cur; while(head1!=null && head2!=null) { if(head1.data<=head2.data) { SinglyLinkedListNode newnode= new SinglyLinkedListNode(head1.data); cur.next=newnode; cur=cur.next; head1=head1.next; } else{ SinglyLinkedListNode newnode= new SinglyLinkedListNode(head2.data); cur.next=newnode; cur=cur.next; head2=head2.next; } } while(head1!=null) { SinglyLinkedListNode newnode= new SinglyLinkedListNode(head1.data); cur.next=newnode; cur=cur.next; head1=head1.next; } while(head2!=null) { SinglyLinkedListNode newnode= new SinglyLinkedListNode(head2.data); cur.next=newnode; cur=cur.next; head2=head2.next; } return head3.next; }
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 →
java code