|
| 1 | +--- |
| 2 | +id: add-two-numbers |
| 3 | +title: Two Sum Problem (LeetCode) |
| 4 | +sidebar_label: 0002 - Add Two Numbers |
| 5 | +tags: |
| 6 | + - Linked List |
| 7 | + - Math |
| 8 | + - Recursion |
| 9 | +description: "This is a solution to the Add Two Numbers problem on LeetCode." |
| 10 | +--- |
| 11 | +## Problem Description |
| 12 | + |
| 13 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 14 | +| :------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------- | |
| 15 | +| [Add Two Numbers on LeetCode](https://leetcode.com/problems/add-two-numbers/) | [Add Two Numbers Solution on LeetCode](https://leetcode.com/problems/add-two-numbers/solutions/5234194/solution/) | [Amruta Jayanti](https://leetcode.com/u/user7669cY/)| |
| 16 | + |
| 17 | + |
| 18 | +## Problem Description |
| 19 | + |
| 20 | +You are given two `non-empty` linked lists representing two non-negative integers. The digits are stored in `reverse order`, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. |
| 21 | + |
| 22 | +You may assume the two numbers do not contain any leading zero, except the number 0 itself. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +**Example 1:** |
| 27 | + |
| 28 | +```plaintext |
| 29 | +Input: l1 = [2,4,3], l2 = [5,6,4] |
| 30 | +Output: [7,0,8] |
| 31 | +Explanation: 342 + 465 = 807. |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2:** |
| 35 | + |
| 36 | +```plaintext |
| 37 | +Input: l1 = [0], l2 = [0] |
| 38 | +Output: [0] |
| 39 | +``` |
| 40 | + |
| 41 | +**Example 3:** |
| 42 | + |
| 43 | +```plaintext |
| 44 | +Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] |
| 45 | +Output: [8,9,9,9,0,0,0,1] |
| 46 | +``` |
| 47 | + |
| 48 | +### Constraints: |
| 49 | + |
| 50 | +- The number of nodes in each linked list is in the range `[1, 100]`. |
| 51 | +- `0 <= Node.val <= 9` |
| 52 | +- It is guaranteed that the list represents a number that does not have leading zeros. |
| 53 | + |
| 54 | + |
| 55 | +## Solution to the problem |
| 56 | + |
| 57 | +### Intuition and Approach |
| 58 | +This is a simple problem. It can be done by maintaining two pointers , each for each linked list. Add the values store them and then move to next node. |
| 59 | + |
| 60 | +#### Implementation |
| 61 | +```python |
| 62 | +# Definition for singly-linked list. |
| 63 | +# class ListNode(object): |
| 64 | +# def __init__(self, val=0, next=None): |
| 65 | +# self.val = val |
| 66 | +# self.next = next |
| 67 | +class Solution(object): |
| 68 | + def addTwoNumbers(self, l1, l2): |
| 69 | + """ |
| 70 | + :type l1: ListNode |
| 71 | + :type l2: ListNode |
| 72 | + :rtype: ListNode |
| 73 | + """ |
| 74 | + carry = 0 |
| 75 | + dummy = ListNode() |
| 76 | + current = dummy |
| 77 | + |
| 78 | + while l1 or l2 or carry: |
| 79 | + # Extract values from the current nodes |
| 80 | + val1 = l1.val if l1 else 0 |
| 81 | + val2 = l2.val if l2 else 0 |
| 82 | + |
| 83 | + # Calculate the sum and carry |
| 84 | + total_sum = val1 + val2 + carry |
| 85 | + carry = total_sum // 10 |
| 86 | + current.next = ListNode(total_sum % 10) |
| 87 | + |
| 88 | + # Move to the next nodes |
| 89 | + if l1: |
| 90 | + l1 = l1.next |
| 91 | + if l2: |
| 92 | + l2 = l2.next |
| 93 | + |
| 94 | + # Move to the next result node |
| 95 | + current = current.next |
| 96 | + |
| 97 | + return dummy.next |
| 98 | +``` |
| 99 | +Above is the implementation in Python. Here total_sum stores the value and adds to the dummy. Variable carry is used to handle the carry bits. |
| 100 | + |
| 101 | +#### Complexity Analysis: |
| 102 | +- Time Complexity : $$O(max(n,m))$$ Here, n,m are the lengths of the input linked lists |
| 103 | +- Space Complexity : $$O(max(n,m))$$ |
| 104 | + |
| 105 | +#### Codes in different languages: |
| 106 | +`CPP`: |
| 107 | +```cpp |
| 108 | +class Solution { |
| 109 | +public: |
| 110 | + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { |
| 111 | + ListNode* dummyHead = new ListNode(0); |
| 112 | + ListNode* tail = dummyHead; |
| 113 | + int carry = 0; |
| 114 | + |
| 115 | + while (l1 != nullptr || l2 != nullptr || carry != 0) { |
| 116 | + int digit1 = (l1 != nullptr) ? l1->val : 0; |
| 117 | + int digit2 = (l2 != nullptr) ? l2->val : 0; |
| 118 | + |
| 119 | + int sum = digit1 + digit2 + carry; |
| 120 | + int digit = sum % 10; |
| 121 | + carry = sum / 10; |
| 122 | + |
| 123 | + ListNode* newNode = new ListNode(digit); |
| 124 | + tail->next = newNode; |
| 125 | + tail = tail->next; |
| 126 | + |
| 127 | + l1 = (l1 != nullptr) ? l1->next : nullptr; |
| 128 | + l2 = (l2 != nullptr) ? l2->next : nullptr; |
| 129 | + } |
| 130 | + |
| 131 | + ListNode* result = dummyHead->next; |
| 132 | + delete dummyHead; |
| 133 | + return result; |
| 134 | + } |
| 135 | +}; |
| 136 | +``` |
| 137 | + |
| 138 | +`Java`: |
| 139 | +```java |
| 140 | +class Solution { |
| 141 | + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { |
| 142 | + ListNode dummyHead = new ListNode(0); |
| 143 | + ListNode tail = dummyHead; |
| 144 | + int carry = 0; |
| 145 | + |
| 146 | + while (l1 != null || l2 != null || carry != 0) { |
| 147 | + int digit1 = (l1 != null) ? l1.val : 0; |
| 148 | + int digit2 = (l2 != null) ? l2.val : 0; |
| 149 | + |
| 150 | + int sum = digit1 + digit2 + carry; |
| 151 | + int digit = sum % 10; |
| 152 | + carry = sum / 10; |
| 153 | + |
| 154 | + ListNode newNode = new ListNode(digit); |
| 155 | + tail.next = newNode; |
| 156 | + tail = tail.next; |
| 157 | + |
| 158 | + l1 = (l1 != null) ? l1.next : null; |
| 159 | + l2 = (l2 != null) ? l2.next : null; |
| 160 | + } |
| 161 | + |
| 162 | + ListNode result = dummyHead.next; |
| 163 | + dummyHead.next = null; |
| 164 | + return result; |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | + |
| 170 | + |
0 commit comments