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

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

Open
KaDiS-Bot opened this issue Mar 13, 2024 · 1 comment
Open

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

KaDiS-Bot opened this issue Mar 13, 2024 · 1 comment

Comments

@KaDiS-Bot
Copy link

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);
}



}
himanshu07rautela added a commit to himanshu07rautela/CodeHelp-DSA-Busted-Series that referenced this issue Mar 30, 2024
its a leetcode answer of merging 2 sorted linked list loveBabbar#536
@himanshu07rautela
Copy link

answer updated , issue solved
question link : https://leetcode.com/problems/merge-two-sorted-lists/description/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants