Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mergeList.cpp #555

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions Lecture049 Linked List Day6/mergeList.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <bits/stdc++.h>

/************************************************************

Following is the linked list node structure.
Expand All @@ -22,50 +24,73 @@

************************************************************/

void solve(Node<int>* first, Node<int>* second) {
Node<int>* solve(Node<int>* first, Node<int>* second) {

//check first LL have only one node

if(first -> next == NULL){

first -> next = second;

return first;

}


Node* curr1 = first;
Node* next1 = curr1 -> next;
Node<int>* curr1 = first;
Node<int>* next1 = curr1 -> next;

Node* curr2 = second;
Node* next2 = curr2 -> next;
Node<int>* curr2 = second;
Node<int>* next2 = curr2 -> next;

while(next1 != NULL && curr2 != NULL) {

if( (curr2 -> data >= curr1 -> data )
&& ( curr2 -> data <= next1 -> data)) {

curr1 -> next = curr2;
curr1 -> next = curr2;
next2 = curr2->next; // Update next2 before moving curr2
curr2 -> next = next1;

// Move curr1 to curr2, which is now the last merged node
curr1 = curr2;
curr2 = next2;
}
else {

}
else{
// move curr1 and next1

curr1=next1;
next1=next1->next;

if(next1==NULL){
curr1->next=curr2;
return first;
}
}


}
return first;


}


Node<int>* sortTwoLists(Node<int>* first, Node<int>* second)
{
// Write your code here.
if(first == NULL)
return second;

if(second == NULL)
return first;

if(first -> data <= second -> data ){
solve(first, second);
return solve(first, second);
}
else
{
solve(second, first);
return solve(second, first);
}


}