You are viewing a single comment's thread. Return to all comments →
SinglyLinkedListNode* head=new SinglyLinkedListNode(0); SinglyLinkedListNode* temp=head; while(head1!=nullptr && head2!=nullptr){ if(head1->data<=head2->data){ SinglyLinkedListNode* newNode=new SinglyLinkedListNode(head1->data); temp->next=newNode; head1=head1->next; }else { SinglyLinkedListNode* newNode=new SinglyLinkedListNode(head2->data); temp->next=newNode; head2=head2->next; }temp=temp->next; } while(head1!=nullptr){ SinglyLinkedListNode* newNode=new SinglyLinkedListNode(head1->data); head1=head1->next; temp->next=newNode; temp=temp->next; } while(head2!=nullptr){ SinglyLinkedListNode* newNode=new SinglyLinkedListNode(head2->data); head2=head2->next; temp->next=newNode; temp=temp->next; }head=head->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 →