Skip to content

Commit

Permalink
Update mergeList.cpp , issue solved , lecture number 49
Browse files Browse the repository at this point in the history
its a leetcode answer of merging 2 sorted linked list loveBabbar#536
  • Loading branch information
himanshu07rautela authored Mar 30, 2024
1 parent b6b8347 commit 0875e5f
Showing 1 changed file with 56 additions and 60 deletions.
116 changes: 56 additions & 60 deletions Lecture049 Linked List Day6/mergeList.cpp
Original file line number Diff line number Diff line change
@@ -1,71 +1,67 @@
/************************************************************
// this is the leetcode solution
class Solution {
public:

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;
void insertattail(ListNode*&head, int data){
ListNode*q=new ListNode();
q->val=data;
q->next=NULL;
ListNode*p=head;
while(p->next!=NULL){
p=p->next;
}
p->next=q;
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode*p=list1;
ListNode*q=list2;
if(p==NULL){
return q;
}
if(q==NULL){
return p;
}
ListNode*ans=new ListNode();
ans->val=-1;
ans->next=NULL;
while(p!=NULL || q!=NULL){
if(p==NULL){
insertattail(ans,q->val);
q=q->next;
continue;

}
if(q==NULL){
insertattail(ans,p->val);
p=p->next;
continue;
}

if((q->val)<(p->val)){
insertattail(ans,q->val);
q=q->next;
continue;

~Node() {
if (next != NULL) {
delete next;
}
}
};
if((q->val)>(p->val)){
insertattail(ans,p->val);
p=p->next;
continue;

}
if((q->val)==(p->val)){
insertattail(ans,p->val);
p=p->next;
insertattail(ans,q->val);
q=q->next;

************************************************************/
}

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)) {

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

}


}


}
return ans->next;

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

if(second == NULL)
return first;

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


}
};

0 comments on commit 0875e5f

Please sign in to comment.