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.
- Merge two sorted linked lists
- Discussions
Merge two sorted linked lists
Merge two sorted linked lists
Sort by
recency
|
218 Discussions
|
Please Login in order to post a comment
SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
if(head1 ==nullptr) { return head2; }
if(head2 == nullptr) { return head1; }
stack temp; while (head1 != nullptr && head2 != nullptr ) {
}
while (head1 != nullptr) { temp.push(head1->data );
}
while (head2 != nullptr) { temp.push(head2->data );
}
SinglyLinkedListNode* res = nullptr;
while(!temp.empty()){
SinglyLinkedListNode *temp = new SinglyLinkedListNode(tp); temp->next = res; res = temp; }
return res;
}
Using a dummy (temporary) node is neat trick to bypass head pointer management. Just don't forget to skip it when finished by returning
head.next
:started with recursive then want to cry :(
function mergeLists(head1, head2) {
}
My answer with Typescrit,
Cause no starting code, i was do it for my self
Questiom sample explain is wrong, it confusing me for a while.