Merge Two Sorted Lists - Leetcode Solution


πŸ’‘ Step-by-Step Thought Process

  1. Understand the problem: Merge two sorted linked lists into one sorted linked list.
  2. Create a dummy node to serve as the starting point of the merged list.
  3. Initialize a current pointer to the dummy node to build the merged list.
  4. While both list1 and list2 have nodes, compare the values of their current nodes.
  5. If list1's value is less than list2's value, attach list1's node to the current pointer, move current to list1's node, and advance list1 to its next node.
  6. Otherwise, attach list2's node to the current pointer, move current to list2's node, and advance list2 to its next node.
  7. After the loop, attach any remaining nodes from list1 or list2 to the current pointer.
  8. Return the next node of the dummy node as the head of the merged list.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the Problem: Merge Two Sorted Lists

The β€œMerge Two Sorted Lists” problem asks us to merge two sorted singly linked lists, list1 and list2, into one new sorted linked list. The goal is to preserve the non-decreasing order of elements and return the head of the newly merged list.

Example:

  • Input: list1 = 1 β†’ 2 β†’ 4, list2 = 1 β†’ 3 β†’ 4
  • Output: 1 β†’ 1 β†’ 2 β†’ 3 β†’ 4 β†’ 4

Why This Problem Matters

Merging two sorted lists is a foundational concept used in algorithms such as merge sort and in real-world scenarios involving sorted streams of data. It reinforces pointer manipulation in linked lists and teaches how to maintain sorted order efficiently.

Optimal Approach: Iterative Merge with a Dummy Node

The most effective way to merge two sorted lists is to use a dummy node to simplify edge cases and a current pointer to build the merged list incrementally.

Steps:

  1. Create a dummy node that will serve as the placeholder for the merged list.
  2. Initialize a current pointer pointing to the dummy node.
  3. While both list1 and list2 are non-null:
    • Compare the values of list1 and list2.
    • Attach the node with the smaller value to current.next.
    • Move current forward and also advance the list from which the node was taken.
  4. After the loop, at most one list will have nodes remaining. Attach the remaining nodes to current.next.
  5. Return dummy.next as the head of the merged list.

Example Walkthrough

Inputs: list1 = 1 β†’ 2 β†’ 4, list2 = 1 β†’ 3 β†’ 4
Execution:

  • Compare 1 and 1 β†’ pick either β†’ attach to merged list
  • Compare 2 and 1 β†’ pick 1 from list2
  • Compare 2 and 3 β†’ pick 2
  • Compare 4 and 3 β†’ pick 3
  • Compare 4 and 4 β†’ pick either
  • Remaining node 4 β†’ attach

Result: 1 β†’ 1 β†’ 2 β†’ 3 β†’ 4 β†’ 4

Time and Space Complexity

Time Complexity: O(n + m), where n is the length of list1 and m is the length of list2. Each node is visited exactly once.
Space Complexity: O(1) auxiliary space, since we are not creating any new nodes (just reusing and rearranging pointers).

Edge Cases to Consider

  • One or both lists are empty β†’ return the non-empty list or null
  • Lists with all duplicate values β†’ handled correctly
  • One list is significantly longer than the other β†’ still works in linear time

Conclusion

The β€œMerge Two Sorted Lists” problem is a classic example of how to manipulate linked lists efficiently using pointers. By reusing existing nodes and carefully adjusting links, we can produce a clean, sorted list with no additional space. This problem forms the backbone of more complex algorithms and teaches critical low-level manipulation skills.

Get Personalized Support at AlgoMap Bootcamp πŸ’‘