|
| 1 | +""" |
| 2 | + Programmer : Dhruv Patel |
| 3 | + Problem Name : Add Two Numbers (LeetCode - Medium) |
| 4 | + Problem => |
| 5 | + You are given two non-empty linked lists representing two non-negative integers. |
| 6 | + The digits are stored in reverse order, and each of their nodes contains a single digit. |
| 7 | + Add the two numbers and return the sum as a linked list. |
| 8 | + You may assume the two numbers do not contain any leading zero, except the number 0 itself. |
| 9 | + Sample TestCases => |
| 10 | + Example 1: |
| 11 | + Input: l1 = [2,4,3], l2 = [5,6,4] |
| 12 | + Output: [7,0,8] |
| 13 | + Explanation: 342 + 465 = 807. |
| 14 | + Example 2: |
| 15 | + Input: l1 = [0], l2 = [0] |
| 16 | + Output: [0] |
| 17 | + Example 3: |
| 18 | + Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] |
| 19 | + Output: [8,9,9,9,0,0,0,1] |
| 20 | + |
| 21 | + Thoughts => We have to travese the linkedlist l1 and l2 and sum elements from both list while traversing. |
| 22 | + while traversing carry will be generated if sum exceeds number 10 . we use carry while doing addition |
| 23 | + in next iteration if any are there and if not then simply append it. There might be a case where lenght of l1 is |
| 24 | + lesser then l2 or vice versa. in that case, we traverse remaining list. |
| 25 | + |
| 26 | + Time - Complexity => |
| 27 | + Worst - O(n) <- where n is representing largest list. |
| 28 | + Space - Complexity => |
| 29 | + Worst - Θ(n) <- where n is representing largest list. |
| 30 | + """ |
| 31 | +class ListNode: |
| 32 | + def __init__(self, val=0, next=None): |
| 33 | + self.val = val |
| 34 | + self.next = next |
| 35 | + |
| 36 | +class Solution: |
| 37 | + |
| 38 | + def calculate(self,answer): |
| 39 | + carry = answer // 10 |
| 40 | + answer = answer % 10 |
| 41 | + return carry,answer |
| 42 | + |
| 43 | + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: |
| 44 | + |
| 45 | + if not l2: |
| 46 | + return l1 |
| 47 | + elif not l1: |
| 48 | + return l2 |
| 49 | + |
| 50 | + temp = None |
| 51 | + carry = 0 |
| 52 | + |
| 53 | + while l1 and l2: |
| 54 | + answer = l1.val + l2.val + carry |
| 55 | + carry = 0 |
| 56 | + if answer > 9: |
| 57 | + carry,answer = self.calculate(answer) |
| 58 | + if not temp: |
| 59 | + temp = ListNode(answer) |
| 60 | + head = temp |
| 61 | + else: |
| 62 | + temp.next = ListNode(answer) |
| 63 | + temp = temp.next |
| 64 | + l1 = l1.next |
| 65 | + l2 = l2.next |
| 66 | + |
| 67 | + while l1: |
| 68 | + answer = l1.val + carry |
| 69 | + carry = 0 |
| 70 | + if answer > 9: |
| 71 | + carry, answer = self.calculate(answer) |
| 72 | + temp.next = ListNode(answer) |
| 73 | + temp = temp.next |
| 74 | + l1 = l1.next |
| 75 | + |
| 76 | + while l2: |
| 77 | + answer = l2.val + carry |
| 78 | + carry = 0 |
| 79 | + if answer > 9: |
| 80 | + carry, answer = self.calculate(answer) |
| 81 | + temp.next = ListNode(answer) |
| 82 | + temp = temp.next |
| 83 | + l2 = l2.next |
| 84 | + |
| 85 | + if carry > 0: |
| 86 | + temp.next = ListNode(carry) |
| 87 | + |
| 88 | + return head |
0 commit comments