Skip to main content

21. Merge Two Sorted Lists

  • Recursion
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2)
{
if (!list1) return list2;
if (!list2) return list1;
if (list1->val < list2->val)
{
list1->next = mergeTwoLists(list1->next, list2);
return list1;
}
else
{
list2->next = mergeTwoLists(list2->next, list1);
return list2;
}
}
};
  • T: O(n+m)O(n + m)

  • S: O(n+m)O(n + m)

  • Iteration

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2)
{
ListNode* dummy = new ListNode(-1);
ListNode* cur = dummy;

while (list1 && list2)
{
if (list1->val < list2->val)
{
cur->next = list1;

list1 = list1->next;
}
else
{
cur->next = list2;
list2 = list2->next;
}
cur = cur->next;
}
if (!list1) cur->next = list2;
if (!list2) cur->next = list1;
return dummy->next;
}
};
  • T: O(n+m)O(n + m)
  • S: O(1)O(1)