1+ # LeetCode 445 - Add Two Numbers II
2+ #
3+ # ---------------------------------------------
4+ # Approach 1: Convert to Integer (Brute Force)
5+ # ---------------------------------------------
6+ # Time Complexity : O(n + m)
7+ # Space Complexity: O(n + m)
8+ #
9+ # Idea:
10+ # 1. Store both linked lists into arrays.
11+ # 2. Convert arrays into integers.
12+ # 3. Add the integers.
13+ # 4. Convert the result back into a linked list.
14+ #
15+ # Note:
16+ # This solution is simple but does NOT satisfy the follow-up
17+ # because it converts the entire number into an integer.
18+
19+
20+ # Definition for singly-linked list.
21+ # class ListNode:
22+ # def __init__(self, val=0, next=None):
23+ # self.val = val
24+ # self.next = next
25+
26+
27+ class SolutionBruteForce :
28+ def addTwoNumbers (self , l1 : Optional [ListNode ], l2 : Optional [ListNode ]) -> Optional [ListNode ]:
29+
30+ n1 = []
31+ curr = l1
32+ while curr :
33+ n1 .append (curr .val )
34+ curr = curr .next
35+
36+ n2 = []
37+ curr = l2
38+ while curr :
39+ n2 .append (curr .val )
40+ curr = curr .next
41+
42+ num1 = int ("" .join (map (str , n1 )))
43+ num2 = int ("" .join (map (str , n2 )))
44+
45+ total = num1 + num2
46+
47+ digits = list (map (int , str (total )))
48+
49+ dummy = ListNode (0 )
50+ curr = dummy
51+
52+ for digit in digits :
53+ curr .next = ListNode (digit )
54+ curr = curr .next
55+
56+ return dummy .next
57+
58+
59+ # ---------------------------------------------------
60+ # Approach 2: Using Two Stacks (Optimal)
61+ # ---------------------------------------------------
62+ # Time Complexity : O(n + m)
63+ # Space Complexity: O(n + m)
64+ #
65+ # Idea:
66+ # 1. Store digits of both linked lists into stacks.
67+ # 2. Pop digits from the end (least significant digit).
68+ # 3. Maintain carry.
69+ # 4. Insert each new node at the FRONT of the answer.
70+ #
71+ # This satisfies the follow-up (no integer conversion).
72+
73+
74+ class Solution :
75+ def addTwoNumbers (self , l1 : Optional [ListNode ], l2 : Optional [ListNode ]) -> Optional [ListNode ]:
76+
77+ stack1 = []
78+ stack2 = []
79+
80+ curr = l1
81+ while curr :
82+ stack1 .append (curr .val )
83+ curr = curr .next
84+
85+ curr = l2
86+ while curr :
87+ stack2 .append (curr .val )
88+ curr = curr .next
89+
90+ carry = 0
91+ dummy = ListNode (0 )
92+
93+ while stack1 or stack2 or carry :
94+
95+ total = carry
96+
97+ if stack1 :
98+ total += stack1 .pop ()
99+
100+ if stack2 :
101+ total += stack2 .pop ()
102+
103+ carry = total // 10
104+ digit = total % 10
105+
106+ # Insert at the beginning
107+ node = ListNode (digit )
108+ node .next = dummy .next
109+ dummy .next = node
110+
111+ return dummy .next
112+
113+
114+ """
115+ Example
116+
117+ Input:
118+ l1 = 7 -> 2 -> 4 -> 3
119+ l2 = 5 -> 6 -> 4
120+
121+ Approach 1:
122+ 7243 + 564 = 7807
123+ Output:
124+ 7 -> 8 -> 0 -> 7
125+
126+ Approach 2:
127+ Stacks:
128+ stack1 = [7, 2, 4, 3]
129+ stack2 = [5, 6, 4]
130+
131+ Pop:
132+ 3 + 4 = 7
133+ 4 + 6 = 10
134+ 2 + 5 + 1 = 8
135+ 7 + 0 = 7
136+
137+ Build answer from front:
138+
139+ 7
140+ ↓
141+
142+ 0 -> 7
143+ ↓
144+
145+ 8 -> 0 -> 7
146+ ↓
147+
148+ 7 -> 8 -> 0 -> 7
149+
150+ Output:
151+ 7 -> 8 -> 0 -> 7
152+ """
0 commit comments