You are viewing a single comment's thread. Return to all comments →
CPP SOLUTION
SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { if(head1==NULL)return head2; if(head2==NULL)return head1; SinglyLinkedListNode*ans=new SinglyLinkedListNode(-1); SinglyLinkedListNode*mptr=ans; while(head1 && head2){ if(head1->data<=head2->data) { mptr->next=head1; mptr=head1; head1=head1->next; } else{ mptr->next=head2; mptr=head2; head2=head2->next; } } if(head1){ mptr->next=head1; } if(head2){ mptr->next=head2; } return ans->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 →
CPP SOLUTION