Advertisement

Add Two Numbers - LeetCode 2 Solution

Add Two Numbers - Complete Solution Guide

Add Two Numbers is LeetCode problem 2, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807. Example 2: Input: l1 = [0], l2 = [0] Output: [0] Example 3: Input: l1 = [9,9,9,9,9,9,9],

Detailed Explanation

The problem asks us to add two numbers represented as linked lists. Each node in the linked list represents a single digit, and the digits are stored in reverse order. We need to return a new linked list representing the sum of the two numbers. For example, if l1 = [2, 4, 3] and l2 = [5, 6, 4], then l1 represents the number 342 and l2 represents the number 465. The sum 342 + 465 = 807, so we should return a linked list [7, 0, 8]. The problem emphasizes that the linked lists are non-empty and represent non-negative integers without leading zeros (except for the number 0 itself).

Solution Approach

The solution iterates through both linked lists simultaneously, adding the digits at each position along with any carry from the previous addition. A new node is created for each digit of the sum and appended to a new linked list. The iteration continues until both linked lists are exhausted and there is no remaining carry. A dummy head node is used to make adding the first node easier.

Step-by-Step Algorithm

  1. Step 1: Initialize a dummy head node for the result linked list and a 'current' pointer to the dummy head.
  2. Step 2: Initialize a 'carry' variable to 0.
  3. Step 3: Iterate while either of the linked lists (l1 or l2) has more nodes or the 'carry' is greater than 0.
  4. Step 4: In each iteration, get the values of the current nodes from l1 and l2. If a linked list is exhausted, use 0 as the value for that position.
  5. Step 5: Calculate the sum of the two values and the 'carry'.
  6. Step 6: Update the 'carry' by taking the integer division of the sum by 10.
  7. Step 7: Calculate the digit to be added to the result linked list by taking the modulus of the sum by 10.
  8. Step 8: Create a new node with the calculated digit and append it to the result linked list by setting current.next to the new node.
  9. Step 9: Move the 'current' pointer to the newly added node.
  10. Step 10: Advance l1 and l2 to their next nodes, or set them to null if they are exhausted.
  11. Step 11: After the loop finishes, return the 'next' of the dummy head, which is the head of the actual result linked list.

Key Insights

  • Insight 1: Use a dummy head node to simplify the creation of the result linked list, avoiding special handling for the first node.
  • Insight 2: Keep track of the carry value from each digit addition to propagate it to the next higher digit.
  • Insight 3: Handle the case where one linked list is longer than the other or when there is a remaining carry after adding all digits.

Complexity Analysis

Time Complexity: O(max(m, n))

Space Complexity: O(max(m, n))

Topics

This problem involves: Linked List, Math, Recursion.

Companies

Asked at: Accenture, Adobe, Airbnb, Amazon, Apple, Avito, Bloomberg, ByteDance, Capgemini, Capital One, Cisco, Cognizant, Commvault, EPAM Systems, EarnIn, Expedia, Goldman Sachs, Google, Infosys, Intel, Meta, Microsoft, Nutanix, Nvidia, Oracle, Samsung, TikTok, Uber, Walmart Labs, Wix, Yahoo, Yandex, Zopsmart, josh technology, tcs.