Leetcode Merge Two Sorted Lists problem solution in C programming

In the Leetcode Merge Two Sorted Lists problem solution in C programming You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into a one-sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

Leetcode Merge Two Sorted Lists problem solution in C programming

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
struct ListNode* ptr=list1;
    if(list1==NULL)
    {
        return list2;
    }
    if(list2==NULL)
    {
        return list1;
    }
    
    if(list1 -> val <= list2 -> val)
    {
        list1 -> next = mergeTwoLists(list1 -> next, list2);
        return list1;
    }

    else
    {
        list2 -> next = mergeTwoLists(list1, list2 -> next);
        return list2;            
    }
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *