|
| 1 | +```python |
| 2 | +# ============================================================ |
| 3 | +# LeetCode 2. Add Two Numbers |
| 4 | +# ============================================================ |
| 5 | + |
| 6 | +# Definition for singly-linked list. |
| 7 | +# class ListNode: |
| 8 | +# def __init__(self, val=0, next=None): |
| 9 | +# self.val = val |
| 10 | +# self.next = next |
| 11 | + |
| 12 | + |
| 13 | +class Solution: |
| 14 | + def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: |
| 15 | + |
| 16 | + # ===================================================== |
| 17 | + # Brute Force Approach (Not Recommended) |
| 18 | + # ----------------------------------------------------- |
| 19 | + # Idea: |
| 20 | + # 1. Store linked list digits in two arrays. |
| 21 | + # 2. Reverse both arrays. |
| 22 | + # 3. Convert them into integers. |
| 23 | + # 4. Add the integers. |
| 24 | + # 5. Convert the sum back into a reversed linked list. |
| 25 | + # |
| 26 | + # Note: |
| 27 | + # This approach may fail for very large inputs because |
| 28 | + # Python has limits when converting extremely large |
| 29 | + # strings into integers. |
| 30 | + # ===================================================== |
| 31 | + |
| 32 | + """ |
| 33 | + n1 = [] |
| 34 | + curr = l1 |
| 35 | + while curr: |
| 36 | + n1.append(curr.val) |
| 37 | + curr = curr.next |
| 38 | +
|
| 39 | + n2 = [] |
| 40 | + curr = l2 |
| 41 | + while curr: |
| 42 | + n2.append(curr.val) |
| 43 | + curr = curr.next |
| 44 | +
|
| 45 | + num1 = int("".join(map(str, n1[::-1]))) |
| 46 | + num2 = int("".join(map(str, n2[::-1]))) |
| 47 | +
|
| 48 | + total = num1 + num2 |
| 49 | +
|
| 50 | + digits = list(str(total))[::-1] |
| 51 | +
|
| 52 | + dummy = ListNode(0) |
| 53 | + curr = dummy |
| 54 | +
|
| 55 | + for digit in digits: |
| 56 | + curr.next = ListNode(int(digit)) |
| 57 | + curr = curr.next |
| 58 | +
|
| 59 | + return dummy.next |
| 60 | + """ |
| 61 | + |
| 62 | + # ===================================================== |
| 63 | + # Optimal Approach |
| 64 | + # ----------------------------------------------------- |
| 65 | + # Idea: |
| 66 | + # Traverse both linked lists simultaneously. |
| 67 | + # Add corresponding digits along with carry. |
| 68 | + # Store (sum % 10) as the current digit. |
| 69 | + # Update carry as (sum // 10). |
| 70 | + # Continue until both lists and carry are exhausted. |
| 71 | + # ===================================================== |
| 72 | + |
| 73 | + dummy = ListNode(0) |
| 74 | + curr = dummy |
| 75 | + carry = 0 |
| 76 | + |
| 77 | + while l1 or l2 or carry: |
| 78 | + |
| 79 | + total = carry |
| 80 | + |
| 81 | + if l1: |
| 82 | + total += l1.val |
| 83 | + l1 = l1.next |
| 84 | + |
| 85 | + if l2: |
| 86 | + total += l2.val |
| 87 | + l2 = l2.next |
| 88 | + |
| 89 | + carry = total // 10 |
| 90 | + digit = total % 10 |
| 91 | + |
| 92 | + curr.next = ListNode(digit) |
| 93 | + curr = curr.next |
| 94 | + |
| 95 | + return dummy.next |
| 96 | + |
| 97 | + |
| 98 | +# ============================================================ |
| 99 | +# Time Complexity |
| 100 | +# ============================================================ |
| 101 | + |
| 102 | +# Brute Force: |
| 103 | +# Time Complexity : O(n + m) |
| 104 | +# Space Complexity : O(n + m) |
| 105 | + |
| 106 | +# Optimal: |
| 107 | +# Time Complexity : O(max(n, m)) |
| 108 | +# Space Complexity : O(1) (excluding the output linked list) |
| 109 | +``` |
0 commit comments