1+ """
2+ LeetCode 21. Merge Two Sorted Lists
3+
4+ Problem:
5+ You are given the heads of two sorted linked lists, list1 and list2.
6+
7+ Merge the two lists into one sorted linked list and return the head
8+ of the merged list.
9+
10+ Example:
11+ Input:
12+ list1 = 1 -> 2 -> 4
13+ list2 = 1 -> 3 -> 4
14+
15+ Output:
16+ 1 -> 1 -> 2 -> 3 -> 4 -> 4
17+
18+ ---------------------------------------------------------
19+ Approach: Recursive
20+ ---------------------------------------------------------
21+
22+ Idea:
23+ Since both linked lists are already sorted, compare the current
24+ nodes of both lists.
25+
26+ 1. If list1's value is smaller (or equal), choose list1's node.
27+ 2. Recursively merge the remaining nodes of list1 with list2.
28+ 3. If list2's value is smaller, choose list2's node.
29+ 4. Recursively merge list1 with the remaining nodes of list2.
30+ 5. Continue until one list becomes empty.
31+
32+ Base Cases:
33+ - If list1 is empty, return list2.
34+ - If list2 is empty, return list1.
35+
36+ Why it works:
37+ At every recursive call, the smallest available node is placed
38+ into the merged list, maintaining sorted order.
39+
40+ ---------------------------------------------------------
41+ Dry Run
42+ ---------------------------------------------------------
43+
44+ list1 = 1 -> 2 -> 4
45+ list2 = 1 -> 3 -> 4
46+
47+ Compare:
48+ 1 <= 1
49+
50+ Choose first 1
51+
52+ 1 -> merge(2->4, 1->3->4)
53+
54+ Compare:
55+ 2 > 1
56+
57+ Choose second 1
58+
59+ 1 -> merge(2->4, 3->4)
60+
61+ Compare:
62+ 2 <= 3
63+
64+ Choose 2
65+
66+ 2 -> merge(4, 3->4)
67+
68+ Compare:
69+ 4 > 3
70+
71+ Choose 3
72+
73+ 3 -> merge(4, 4)
74+
75+ Compare:
76+ 4 <= 4
77+
78+ Choose first 4
79+
80+ 4 -> merge(None, 4)
81+
82+ Return remaining list:
83+
84+ 4
85+
86+ Final Result:
87+
88+ 1 -> 1 -> 2 -> 3 -> 4 -> 4
89+
90+ ---------------------------------------------------------
91+ Time Complexity: O(m + n)
92+ ---------------------------------------------------------
93+ m = length of list1
94+ n = length of list2
95+
96+ Each node is visited exactly once.
97+
98+ ---------------------------------------------------------
99+ Space Complexity: O(m + n)
100+ ---------------------------------------------------------
101+ Recursive call stack can grow up to m + n calls.
102+
103+ ---------------------------------------------------------
104+ """
105+
106+ # Definition for singly-linked list.
107+ # class ListNode:
108+ # def __init__(self, val=0, next=None):
109+ # self.val = val
110+ # self.next = next
111+
112+ class Solution :
113+ def mergeTwoLists (
114+ self ,
115+ list1 : Optional [ListNode ],
116+ list2 : Optional [ListNode ]
117+ ) -> Optional [ListNode ]:
118+
119+ # Base Case 1:
120+ # If list1 is empty, return list2
121+ if list1 is None :
122+ return list2
123+
124+ # Base Case 2:
125+ # If list2 is empty, return list1
126+ if list2 is None :
127+ return list1
128+
129+ # Choose the smaller node
130+ if list1 .val <= list2 .val :
131+
132+ # Merge the remaining nodes
133+ list1 .next = self .mergeTwoLists (
134+ list1 .next ,
135+ list2
136+ )
137+
138+ return list1
139+
140+ else :
141+
142+ # Merge the remaining nodes
143+ list2 .next = self .mergeTwoLists (
144+ list1 ,
145+ list2 .next
146+ )
147+
148+ return list2
0 commit comments