Add Two Numbers LeetCode Solution

Here, We see Add Two Numbers problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python etc. with different approach.

Add Two Numbers LeetCode Solution

Add Two Numbers LeetCode Solution

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.

Add Two Numbers LeetCode Solution example
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], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Approach : Elementary Math :-

Algorithm :-

Fig – Visualization of the addition of two numbers: 342 + 465 = 807
Each node contains a single digit and the digits are stored in reverse order.

Pseudocode :-

  • Initialize current node to dummy head of the returning list.
  • Initialize carry to 0.
  • Initialize p and q to head of l1 and l2 respectively.
  • Loop through lists l1 and l2 until you reach both ends.
    • Set x to node p‘s value. If pp has reached the end of l1, set to 0.
    • Set y to node q‘s value. If q has reached the end of l2, set to 0.
    • Set sum = x+y+carry.
    • Update carry=sum/10.
    • Create a new node with the digit value of (sum mod 10) and set it to current node’s next, then advance current node to next.
    • Advance both p and q.
  • Check if carry=1, if so append a new node with digit 1 to the returning list.
  • Return dummy head’s next node.

Java Solution ->

public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0); ListNode p = l1, q = l2, curr = dummyHead; int carry = 0; while (p != null || q != null) { int x = (p != null) ? p.val : 0; int y = (q != null) ? q.val : 0; int sum = carry + x + y; carry = sum / 10; curr.next = new ListNode(sum % 10); curr = curr.next; if (p != null) p = p.next; if (q != null) q = q.next; } if (carry > 0) { curr.next = new ListNode(carry); } return dummyHead.next; }
Code language: Java (java)

C++ Solution ->

class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int sum=0; ListNode *l3=NULL; ListNode **node=&l3; while(l1!=NULL||l2!=NULL||sum>0) { if(l1!=NULL) { sum+=l1->val; l1=l1->next; } if(l2!=NULL) { sum+=l2->val; l2=l2->next; } (*node)=new ListNode(sum%10); sum/=10; node=&((*node)->next); } return l3; } };
Code language: C++ (cpp)

JavaScript Solution ->

var addTwoNumbers = function(l1, l2) { let node = null const carry = arguments[2] if (l1 || l2) { const val1 = l1 ? l1.val : 0 const val2 = l2 ? l2.val : 0 const next1 = l1 ? l1.next : null const next2 = l2 ? l2.next : null const val = carry ? val1 + val2 + 1 : val1 + val2 node = new ListNode(val % 10) node.next = addTwoNumbers(next1, next2, val >= 10) } else if (carry) { node = new ListNode(1) node.next = null } return node };
Code language: JavaScript (javascript)

Python Solution ->

class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ result = ListNode(0) result_tail = result carry = 0 while l1 or l2 or carry: val1 = (l1.val if l1 else 0) val2 = (l2.val if l2 else 0) carry, out = divmod(val1+val2 + carry, 10) result_tail.next = ListNode(out) result_tail = result_tail.next l1 = (l1.next if l1 else None) l2 = (l2.next if l2 else None) return result.next
Code language: Python (python)

Add Two Numbers, Add Two Numbers Leetcode, Add Two Numbers Leetcode solution, leetcode Add Two Numbers solution, Leetcode Add Two Numbers, Add Two Numbers python solution, Add Two Numbers java solution, Add Two Numbers JavaScript solution, Add Two Numbers C++ solution

Best Time to Buy and Sell Stock II

Here, We see Best Time to Buy and Sell Stock II problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python …
Read More

Remove Duplicates from Sorted Array

Here, We see Remove Duplicates from Sorted Array problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python etc. List of …
Read More

Two Sum LeetCode Solution

Here, We see Two Sum LeetCode problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python etc. with different approach. List …
Read More

Leave a Comment

Your email address will not be published.