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,
- Leetcode Merge Two Sorted Lists problem solution in C++
- Leetcode Merge Two Sorted Lists problem solution in Java
- Leetcode Merge Two Sorted Lists problem solution in Python
- Leetcode Merge Two Sorted Lists problem solution in C#