Data Structures & Algorithms

Leetcode 2 Add Two Numbers Java Solution

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 contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

Solution 1:
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
         if(l1 == null && l2 == null) return null;
      Queue<Integer> s1 = new LinkedList<Integer>();
       Queue<Integer> s2 = new LinkedList<Integer>();
       ListNode returnNode = null;
       ListNode firstNode = null;
       int carry = 0;
       
       while(l1!=null){
           s1.add(l1.val);
           l1 = l1.next;
       }
       while(l2!=null){
           s2.add(l2.val);
           l2 = l2.next;
       }
       while(s1.peek()!=null || s2.peek()!=null){
           int s1pop = 0;
           int s2pop = 0;
           if(s1.peek()!=null){
            s1pop = (Integer)s1.remove();
           }
           if(s2.peek()!=null){
            s2pop = (Integer)s2.remove();
           }
           int total = s1pop + s2pop + carry;
           if(total >= 10){
               carry = total/10;
               ListNode n = new ListNode(total%10);
               if(returnNode == null){
                   returnNode = n;
                   firstNode = returnNode;
               }else{
                   while(returnNode.next!=null){
                       returnNode= returnNode.next;
                   }
                   returnNode.next = n;
               }
           }else{
               carry = 0;
               ListNode n2 = new ListNode(total);
                if(returnNode == null){
                   returnNode = n2;
                   firstNode = returnNode;
               }else{
                   while(returnNode.next!=null){
                       returnNode= returnNode.next;
                   }
                   returnNode.next = n2;
               }
               
           }
       }
       if(carry!=0){
         ListNode carryNode =   new ListNode(carry);
         while(returnNode.next!=null){
             returnNode = returnNode.next;
         }
           returnNode.next = carryNode;
       }
       return firstNode; 
    }
}
rajendra

Recent Posts

Largest Unique Number Java Solution

Question : Given an array of integers A, return the largest integer that only occurs once.…

10 months ago

Jump Search Algorithm In Java

Jump search algorithm is a pretty new algorithm to search for an element in a…

1 year ago

Knuth Morris Pratt Pattern Search Algorithm

What is Knuth Morris Pratt or KMP algorithm ? KMP is an algorithm which is…

1 year ago

Binary Search Algorithm In Java

Binary Search is a Logarithmic search which finds the target element in a sorted array…

1 year ago

Leetcode Integer to Roman Java Solution

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X…

2 years ago

Leetcode Container With Most Water Java Solution

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such…

2 years ago

This website uses cookies.