Merge two sorted linked lists | Practice | GeeksforGeeks

Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list.
 
Example 1:
Input:
N = 4, M = 3 
valueN[] = {5,10,15,40}
valueM[] = {2,3,20}
Output: 2 3 5 10 15 20 40
Explanation: After merging the two linked
lists, we have merged list as 2, 3, 5,
10, 15, 20, 40.

Example 2:
Input:
N = 2, M = 2
valueN[] = {1,1}
valueM[] = {2,4}
Output:1 1 2 4
Explanation: After merging the given two
linked list , we have 1, 1, 2, 4 as
output.
Your Task:
The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list.
Expected Time Complexity : O(n+m)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N, M <= 104
1 <= Node's data <= 105
0
swapniltayal4226 days ago
Node* sortedMerge(Node* head1, Node* head2)  {     Node* temp1 = head1;   Node* temp2 = head2;   Node* dummynode = new Node(-1);   Node* ans = dummynode;
   while(temp1 != NULL && temp2 != NULL){       if (temp1->data < temp2->data){           ans->next = temp1;           temp1 = temp1->next;       }       else{           ans->next = temp2;           temp2 = temp2->next;       }       ans = ans->next;   }      while (temp1 != NULL){       ans->next = temp1;       temp1 = temp1->next;       ans = ans->next;   }   while (temp2 != NULL){       ans->next = temp2;       temp2 = temp2->next;       ans = ans->next;   }      return dummynode->next;}  
0
sudeepgarg6716 days ago
Node* sortedMerge(Node* head1, Node* head2)  {   Node *p1 = head1;   Node *p2 = head2;   Node *nodedummy = new Node(-1);   Node *p3 = nodedummy;   while(p1!=NULL && p2!=NULL){       if(p1->data<=p2->data){           p3->next = p1;           p1 = p1->next;       }       else{           p3->next = p2;           p2 = p2->next;       }       p3 = p3->next;   }   while(p1!=NULL){           p3->next=p1;           p1 = p1->next;           p3 = p3->next;       }   while(p2!=NULL){       p3->next=p2;       p2 = p2->next;       p3 = p3->next;   }   return nodedummy->next;} 
0
rishu6 days ago
Node result,small,large,prev=null,s_next;
    if(head1.data<=head2.data){
        small=head1;
        large=head2;
    }
    else{
        small=head2;
        large=head1;
    }
    result = large;
    
    while(small!=null && large!=null){
        if(small.data<=large.data){
            s_next = small.next;
            small.next=large;
            if(prev==null){
                prev=small;
                result = small;
            }
            else{
                prev.next=small;
                prev=prev.next;
            }
            small=s_next;
        }
        else{
           prev=large;
           large=large.next; 
        }
        
    }
    if(large==null){
        prev.next = small;
    }
    return result;
0
shakshamkaushik16 days ago
// 
class LinkedList{      Node sortedMerge(Node head1, Node head2) {   Node curr1 = head1;   Node curr2 = head2;   Node dummy = new Node(0);   Node ans = dummy;      while(curr1!=null && curr2!=null){       if(curr1.data<=curr2.data){           
           dummy.next = new Node(curr1.data);           dummy = dummy.next;           curr1 = curr1.next;       }       else{                   dummy.next = new Node(curr2.data);           dummy = dummy.next;           curr2 = curr2.next;   }   }   while(curr1!=null){       dummy.next = new Node(curr1.data);       dummy = dummy.next;       curr1 = curr1.next;   }   while(curr2!=null){       dummy.next = new Node(curr2.data);       dummy = dummy.next;       curr2 = curr2.next;   }   return ans.next;}} 
0
09himanshusah1 week ago
JavaScript Solution
class Solution {
    //Function to merge two sorted linked list.
    sortedMerge(head1, head2)
    {
        //your code here
        let l1 = head1;
        let l2 = head2;
        let dummy = new Node(-1);
        let tail = dummy;
        if(l1 == null) return l2;
        if(l2 == null) return l1;
        
        while(l1 != null && l2 != null) {
            if(l1.data <= l2.data) {
                tail.next = l1;
                tail = l1;
                l1 = l1.next;
            } else {
                tail.next = l2;
                tail = l2;
                l2 = l2.next;
            }
        }
        if(l1 == null) tail.next = l2;
        else tail.next = l1;
        return dummy.next;
    }
}
0
saicharanthammi1 week ago
Node* sortedMerge(Node* head1, Node* head2)  { Node * start=new Node(-1);Node * temp=start;while(!(head1==NULL || head2==NULL)){   int x;Node* del;    if(head1->data<=head2->data)     {x=head1->data;del=head1;head1=head1->next;}     else     {x=head2->data; del=head2;head2=head2->next;}     delete(del);     Node * t=new Node(x);     temp->next=t;     temp=temp->next;}if(!head1)   temp->next=head2;else temp->next=head1;return start->next;    }  
0
hmig9d2 weeks ago
My code didn't work. I need help to understand the issue. 
 
class LinkedList{   //Function to merge two sorted linked list.   Node sortedMerge(Node head1, Node head2) {       Node beforeHead = new Node(-1);       Node temp = beforeHead;       temp = temp.next;       while(head1!=null && head2!=null){           if(head1.data<head2.data){               temp = head1;               head1 = head1.next;           }else{               temp = head2;               head2 = head2.next;           }           //System.out.println(temp.data);           //System.out.println(temp.next);           temp = temp.next;       }              while(head1!=null){           temp = head1;           head1 = head1.next;           temp = temp.next;       }              while(head2!=null){           temp = head2;           head2 = head2.next;           temp = temp.next;       }             //System.out.println(beforeHead.next);       return beforeHead.next;  } }
 
 
 
 
0
riyaraip082 weeks ago
C++ SOLUTION
 
Node* temp = new Node(0);   Node* res = temp;      while(head1!=NULL && head2!=NULL)   {       if(head1->data < head2->data){           temp->next = head1 ;           temp = temp->next;           head1 = head1->next;       }       else{           temp->next = head2 ;           temp = temp->next;           head2 = head2->next;       }   }   if(head1) temp->next = head1;   else temp->next = head2;   return res->next;
0
tthakare732 weeks ago
//Java Solution
    Node sortedMerge(Node head1, Node head2) {
        Node Result = null, dummy = null;
        while(head1 != null && head2 != null){
                if(head1.data < head2.data){
                    Node newNode = head1;
                    head1 = head1.next;
                    if(Result == null){
                      Result = newNode;  
                    } 
                    
                    if(dummy == null){
                        dummy = newNode;
                    } else {
                        dummy.next = newNode;
                        dummy = dummy.next;
                    }
                } else {
                    Node newNode = head2;
                    head2 = head2.next;
                    if(Result == null){
                      Result = newNode;  
                    } 
                    
                    if(dummy == null){
                        dummy = newNode;
                    } else {
                        dummy.next = newNode;
                        dummy = dummy.next;
                    }
                }
        }
        
        if(head1 == null) dummy.next = head2;
        if(head2 == null) dummy.next = head1;
        return Result;
   } 
0
jagannath23sahoo2 weeks ago
C++
TC= O(n+m)
Aux SC: O(1)
//Function to merge two sorted linked list.
Node* sortedMerge(Node* head1, Node* head2)  
{
    if(head1 == NULL)   return head2;
    if(head2 == NULL)   return head1;
    Node* head = NULL;
    Node* tail = NULL;
    if(head1->data <= head2->data)
    {
        head = head1;
        tail = head1;
        head1 = head1->next;
    }
    else{
        head = head2;
        tail = head2;
        head2 = head2->next;
    }
    //Till Both of LL have elements
    while(head1 != NULL and head2 != NULL)
    {
        if(head1->data <= head2->data)
        {
            tail->next = head1;
            tail = head1;
            head1 = head1->next;
        }
        else{
            tail->next = head2;
            tail = head2;
            head2 = head2->next;
        }
    }
    //If head1 is exhusted
    if(head1 == NULL)
    {
        while(head2 != NULL)
        {
            tail->next = head2;
            tail = head2;
            head2 = head2->next;
        }
    }
    else if(head2 == NULL)
    {//Head2 is exhusted
        while(head1 != NULL)
        {
            tail->next = head1;
            tail = head1;
            head1 = head1->next;
        }
    }
    return head;
    
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.