Add Two Numbers Leetcode Solution

Add Two Numbers Leetcode Problem :

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.

C Program to Add Two Matrices

Add Two Numbers Leetcode Solution :

Constraints :

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros

Example 1:

Input: l1 = [0], l2 = [0]
Output: [0]

Example 2:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Intuition:

The Intuition is to iterate through two linked lists representing non-negative integers in reverse order, starting from the least significant digit. It performs digit-wise addition along with a carry value and constructs a new linked list to represent the sum. The process continues until both input lists and the carry value are exhausted. The resulting linked list represents the sum of the input numbers in the correct order.

Explanation:

  1. Create a placeholder node called dummyHead with a value of 0. This node will hold the resulting linked list.
  2. Initialize a pointer called tail and set it to dummyHead. This pointer will keep track of the last node in the result list.
  3. Initialize a variable called carry to 0. This variable will store the carry value during addition.
  4. Start a loop that continues until there are no more digits in both input lists (l1 and l2) and there is no remaining carry value.
  5. Inside the loop:
    • Check if there is a digit in the current node of l1. If it exists, assign its value to a variable called digit1. Otherwise, set digit1 to 0.
    • Check if there is a digit in the current node of l2. If it exists, assign its value to a variable called digit2. Otherwise, set digit2 to 0.
    • Add the current digits from l1 and l2, along with the carry value from the previous iteration, and store the sum in a variable called sum.
    • Calculate the unit digit of sum by taking the modulus (%) of sum by 10. This digit will be placed in a new node for the result.
    • Update the carry variable by dividing sum by 10 and taking the integer division part. This gives us the carry value for the next iteration.
    • Create a new node with the calculated digit as its value.
    • Attach the new node to the tail node of the result list.
    • Move the tail pointer to the newly added node.
    • Move to the next nodes in both l1 and l2, if they exist. If either list is exhausted, set the corresponding pointer to nullptr.
  6. After the loop, obtain the actual result list by skipping the dummyHead node.
  7. Delete the dummyHead node.
  8. Return the resulting list.
Add two numbers leetcode

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription