SHUBHAM SHARMA
SHUBHAM SHARMA

Reputation: 3

Adding two numbers represented as linked list

I'm trying to implement a function to add two numbers represented as reverse linked lists. In my opinion the code is correct but on giving input with both linkedlist consisting of just one number [5] . The output is coming out to be [0], but it's supposed to be [0]->[1]

Example First List: 5->6->3 // represents number 365 Second List: 8->4->2 // represents number 248 . Resultant list: 3->1->6 // represents number 613

can someone tell me what I'm doing wrong in the logic?`

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //find length
        int carry = 0;
        ListNode sum = null;
        int x = 0;
        ListNode top = new ListNode(0);
        while(l1 != null || l2 != null){
            sum = new ListNode((l1.val+l2.val+carry)%10);
//            sum.val = (l1+l2+carry)%10;
            if(x==0){
                top.next = sum;
                x++;
            }
            carry = (l1.val+l2.val)/10;
            l1 = l1.next;
            l2 = l2.next;
            sum = sum.next;
        }
        if(l1 == null && l2 == null){
            //return top;

        }else if(l1 != null && l2 == null){
            while(l1 != null){
                sum = new ListNode((l1.val+carry)%10);
//            sum.val = (l1+carry)%10;
            if(x==0){
                top.next = sum;
                x++;
            }
            carry = (l1.val)/10;
            l1 = l1.next;
            sum = sum.next;
            }
            //return top;
        }else{
            while(l1 != null){
            sum = new ListNode((l2.val+carry)%10);
//            sum.val = (l2+carry)%10;
            if(x==0){
                top.next = sum;
                x++;
            }
            carry = (l2.val)/10;
            l2 = l2.next;
            sum = sum.next;
            }
            //return top;
        }
        if(carry == 1){
            sum = new ListNode(1);

            sum = sum.next;
        }
        return top.next;
    }
}`

Upvotes: 0

Views: 2271

Answers (2)

user180100
user180100

Reputation:

Here's a solution (using a "value" method for ListNode):

package so30544570;

public class Solution {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        return ListNode.valueOf(l1.value() + l2.value());
    }

    public static void main(String[] args) {
        ListNode l1 = ListNode.valueOf(365);
        ListNode l2 = ListNode.valueOf(248);
        System.out.println("l1 = " + l1);
        System.out.println("l2 = " + l2);
        System.out.println("l1 + l2 = " + addTwoNumbers(l1, l2));
    }
}

And ListNode:

package so30544570;

public class ListNode {
    private int val;
    private ListNode next;

    private ListNode(int x) {
        val = x;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder().append(val);
        if (next != null) {
            sb.append(" -> ").append(next.toString());
        }
        return sb.toString();
    }

    public int value() {
        int result = val;
        if (next != null) {
            result += 10 * next.value();
        }
        return result;
    }

    public static ListNode valueOf(final int i) {
        ListNode root = new ListNode(i % 10);
        if (i / 10 > 0) {
            root.next = ListNode.valueOf(i / 10);
        }
        return root;
    }
}

Output:

l1 = 5 -> 6 -> 3

l2 = 8 -> 4 -> 2

l1 + l2 = 3 -> 1 -> 6


NB: this solution avoids the sum computation, we just recreate a ListNode for the sum.

Upvotes: 1

shanmuga
shanmuga

Reputation: 4499

You are never updating top. What you should be doing is

if(carry == 1){
            sum = new ListNode(1);
            if(x==0){
                top.next = sum;   //This line is not executed as x = 1
                x++;
        }

What is x and why is it used.

Note: Name your variable properly for readability.

Upvotes: 0

Related Questions