Skip to content
Closed
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
67 changes: 39 additions & 28 deletions Lecture049 Linked List Day6/mergeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,61 @@

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

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


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

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

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

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

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

//if only one element is present in first list
if(first -> next == NULL) {
first -> next = second;
return first;
}

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

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

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

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

//add node in between the first list
curr1 -> next = curr2;
next2 = curr2 -> next;
curr2 -> next = next1;
//update pointers

curr1 = curr2;
curr2 = next2;
}
else {

else{
//go one step ahead in first list
curr1 = next1;
next1 = next1 -> next;

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


}


return first;
}

Node<int>* sortTwoLists(Node<int>* first, Node<int>* second)
{
if(first == NULL)
if(first == NULL)
return second;

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

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


}