01 Jan 2024C++ / Python / TypeScriptEasy

merge two sorted lists

merge two sorted lists

LeetCode 21: Merge Two Sorted Lists

i recently solved the merge two sorted lists problem on leetcode, and it's a great example of linked list techniques. this easy problem tests your understanding of linked list.

Problem Statement

[problem description would go here - you can add the actual problem statement]

My Approach

when i first saw this problem, i immediately thought about using iterative merge. the key insight is [to be filled based on the specific problem].

Initial Thoughts

i started by thinking about different approaches:

  • **brute force** - [brute force approach description]
  • **optimized approach** - Iterative Merge
  • **alternative approach** - [alternative approach if applicable]

Solution Strategy

i decided to use a Iterative Merge with the following strategy:

  • **[step 1]** - [description]
  • **[step 2]** - [description]
  • **[step 3]** - [description]

My Solution

def solution(self, params):
# implementation would go here
pass

Code Breakdown

let me walk through how this solution works:

1. [Section 1]

# code snippet

[explanation]

2. [Section 2]

# code snippet

[explanation]

Example Walkthrough

let's trace through an example:

[step-by-step walkthrough]

Time and Space Complexity

  • **time complexity:** [complexity analysis]
  • **space complexity:** [complexity analysis]

Key Insights

  • **[insight 1]** - [explanation]
  • **[insight 2]** - [explanation]
  • **[insight 3]** - [explanation]

Alternative Approaches

i also considered:

  • **[alternative 1]** - [description]
  • **[alternative 2]** - [description]

Edge Cases to Consider

  • **[edge case 1]** - [description]
  • **[edge case 2]** - [description]
  • **[edge case 3]** - [description]

Lessons Learned

this problem taught me:

  • [lesson 1]
  • [lesson 2]
  • [lesson 3]

Conclusion

the merge two sorted lists problem is a great exercise in linked list. the key insight is [key takeaway].

you can find my complete solution on [leetcode](https://leetcode.com/problems/merge-two-sorted-lists/solutions/1234569/merge-two-sorted-lists-solution-by-1cbyc).

---

*this is part of my leetcode problem-solving series. i'm documenting my solutions to help others learn and to track my own progress.*

Solution files

Pythonmerge-two-sorted-lists/synced-solution.py
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode()
        cur = dummy
        while list1 and list2:
            if list1.val <= list2.val:
                cur.next = list1; list1 = list1.next
            else:
                cur.next = list2; list2 = list2.next
            cur = cur.next
        cur.next = list1 or list2
        return dummy.next
C++merge-two-sorted-lists/synced-solution.cpp
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode dummy; ListNode* cur = &dummy;
        while (l1 && l2) {
            if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; }
            else { cur->next = l2; l2 = l2->next; }
            cur = cur->next;
        }
        cur->next = l1 ? l1 : l2;
        return dummy.next;
    }
};
TypeScriptmerge-two-sorted-lists/synced-solution.ts
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
    const dummy = new ListNode();
    let 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!;
    }
    cur.next = list1 ?? list2;
    return dummy.next;
}