Skip to content

Issue in Code (Lecture-49 , Merge 2 sorted lists) #536

@KaDiS-Bot

Description

@KaDiS-Bot

While solving the problem , the code provided in GitHub is not relevant.

#include <bits/stdc++.h>

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

Following is the linked list node structure.

template <typename T>
class Node {
    public:
    T data;
    Node* next;

    Node(T data) {
        next = NULL;
        this->data = data;
    }

    ~Node() {
        if (next != NULL) {
            delete next;
        }
    }
};

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

Node* solve(Node* first,Node* second){
Node* curr1=first;
Node* next1=curr1->next;
Node* curr2=second;
Node* next2=curr2->next;
if(next1==NULL){
curr1->next=curr2;
return first;
}
while(next1!=NULL && curr2!=NULL){
if((curr1->data<=curr2->data)&&(next1->data>=curr2->data)){
curr1->next=curr2;
next2=curr2->next;
curr2->next=next1;
// curr2->next=next1;
curr1=curr2;
curr2=next2;
// next2=next2->next;

    }
    else{
        
        curr1=curr1->next;
        next1=next1->next;

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

}

Node* sortTwoLists(Node* first, Node* second)
{
if(first==NULL){
return second;
}
if(second==NULL){
return first;
}

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



}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions