0

The problem I am trying to solve is a LeetCode problem, but I am stuck https://leetcode.com/problems/add-two-numbers/

I have tried the following

import java.util.*;
class Solution {
    private ListNode l = new ListNode(0);
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        int pow = 0;
        int counter = 0;
        int sum1 = 0;
        int sum2 = 0;


        while(l1 != null && l2 != null)
        {
            sum1 = sum1 + ((int) (l1.val * (Math.pow(10,pow))));
            sum2 = sum2 + ((int) (l2.val * (Math.pow(10,pow))));
            pow++;
            l1 = l1.next;
            l2 = l2.next;
        }

        pow = pow - 1;

        int final_sum = sum1 + sum2;
        String number = String.valueOf(final_sum);
        char[] digits = number.toCharArray();


        l = l.next;
        ListNode x = new ListNode();
        x.val = (digits[2] - '0');
        l= x;
        addElement(digits[1] - '0');
        addElement(digits[0] - '0');

        return l;


    }

    public void addElement(int number)
    {
        ListNode x = new ListNode();
        x.val = number;  

        l.next = x;
    }
}

However, I noticed that this just replaces the last given value of ListNode x and does not add on to ListNode l. I am first trying to make it work with one instance of a ListNode without creating multiple "ListNode x = new ListNode()" with different variable names.

At the moment it is just returning [7,8] when given [2,4,3] and [5,6,4] when it should be returning [7,0,8]. Any hints or advice would help.

Rey Sarmiento
  • 11
  • 1
  • 3

2 Answers2

2

Rey, do not make it complex, this is just an example

start with a null node;

ListNode l3 = null;

define a rem and the temp node;

int rem = 0;
ListNode temp = l3;

iterate the both nodes;

while(l1 != null && l2 != null)      

sum the vals,

int sum = l1.val + l2.val;

if temp is null, you are at the beginning;

l3 = temp = new ListNode(sum%10);
rem = sum / 10;

if not, calculate next, carry if rem

sum += rem;
rem = sum / 10; 
temp.next = new ListNode(sum%10);

move temp

temp = temp.next;

also move sources,

l1 = l1.next;
l2 = l2.next;

when you finished, carry if rem again,

if (rem > 0) {
    temp.next = new ListNode(rem);
}

and return;

return l3;
0

Solution with the help of recursion

class solution{
    int carry =0;

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

    // create a base case where recursion terminates

    if (l1 == null && l2 == null && carry == 0) {
        return null;
    }

    // calculate sum of current node and get carry variable updated

    int val1 = l1 == null ? 0 : l1.val;
    int val2 = l2 == null ? 0 : l2.val;

    int sum = val1 + val2 + carry;
    carry = sum/10;

    // update l1,l2 with next node if occur 

    l1 = l1 == null ? null : l1.next;
    l2 = l2 == null ? null : l2.next;

    //perform recursion and store result in new Listnode and.
    
    ListNode ans = new ListNode(sum%10, addTwoNumbers(l1, l2));
    
    return ans;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350