|
| 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 | +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. |
| 14 | + |
| 15 | +You may assume the two numbers do not contain any leading zero, except the number 0 itself. |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +Example 1: |
| 20 | + |
| 21 | + |
| 22 | +Input: l1 = [2,4,3], l2 = [5,6,4] |
| 23 | +Output: [7,0,8] |
| 24 | +Explanation: 342 + 465 = 807. |
| 25 | +Example 2: |
| 26 | + |
| 27 | +Input: l1 = [0], l2 = [0] |
| 28 | +Output: [0] |
| 29 | +Example 3: |
| 30 | + |
| 31 | +Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] |
| 32 | +Output: [8,9,9,9,0,0,0,1] |
| 33 | + |
| 34 | + |
| 35 | +Constraints: |
| 36 | + |
| 37 | +The number of nodes in each linked list is in the range [1, 100]. |
| 38 | +0 <= Node.val <= 9 |
| 39 | +It is guaranteed that the list represents a number that does not have leading zeros. |
| 40 | + |
| 41 | + |
| 42 | +## Solution to the problem |
| 43 | + |
| 44 | +### Intuition and Approach |
| 45 | +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. |
| 46 | + |
| 47 | +#### Implementation |
| 48 | +```python |
| 49 | +# Definition for singly-linked list. |
| 50 | +# class ListNode(object): |
| 51 | +# def __init__(self, val=0, next=None): |
| 52 | +# self.val = val |
| 53 | +# self.next = next |
| 54 | +class Solution(object): |
| 55 | + def addTwoNumbers(self, l1, l2): |
| 56 | + """ |
| 57 | + :type l1: ListNode |
| 58 | + :type l2: ListNode |
| 59 | + :rtype: ListNode |
| 60 | + """ |
| 61 | + carry = 0 |
| 62 | + dummy = ListNode() |
| 63 | + current = dummy |
| 64 | + |
| 65 | + while l1 or l2 or carry: |
| 66 | + # Extract values from the current nodes |
| 67 | + val1 = l1.val if l1 else 0 |
| 68 | + val2 = l2.val if l2 else 0 |
| 69 | + |
| 70 | + # Calculate the sum and carry |
| 71 | + total_sum = val1 + val2 + carry |
| 72 | + carry = total_sum // 10 |
| 73 | + current.next = ListNode(total_sum % 10) |
| 74 | + |
| 75 | + # Move to the next nodes |
| 76 | + if l1: |
| 77 | + l1 = l1.next |
| 78 | + if l2: |
| 79 | + l2 = l2.next |
| 80 | + |
| 81 | + # Move to the next result node |
| 82 | + current = current.next |
| 83 | + |
| 84 | + return dummy.next |
| 85 | +``` |
| 86 | +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. |
| 87 | + |
| 88 | +#### Complexity Analysis: |
| 89 | +- Time Complexity : $$O(max(n,m))$$ .Here, n,m are the lengths of the input linked lists |
| 90 | +- Space Complexity : $$O(max(n,m))$$ |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments