Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions C++ BASIC COURSE/Merge Two Sorted Lists.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
{
// if list1 happen to be NULL
// we will simply return list2.
if(l1 == NULL)
{
return l2;
}

// if list2 happen to be NULL
// we will simply return list1.
if(l2 == NULL)
{
return l1;
}

// if value pointend by l1 pointer is less than equal to value pointed by l2 pointer
// we wall call recursively l1 -> next and whole l2 list.
if(l1 -> val <= l2 -> val)
{
l1 -> next = mergeTwoLists(l1 -> next, l2);
return l1;
}
// we will call recursive l1 whole list and l2 -> next
else
{
l2 -> next = mergeTwoLists(l1, l2 -> next);
return l2;
}
}
};