0

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;
    }
}`
5
  • I find your explanation a bit confusing... Commented May 30, 2015 at 9:43
  • First List: 5->6->3 // represents number 365 Second List: 8->4->2 // represents number 248 . Resultant list: 3->1->6 // represents number 613 Commented May 30, 2015 at 10:03
  • ok, edit your question based on that, make it simple for someone who wants to help you to understand your problem Commented May 30, 2015 at 10:08
  • please post your full code with insertion and main method so its easy to find the mistake Commented May 30, 2015 at 10:53
  • There's no full code. I'm solving this problem here --> leetcode.com/problems/add-two-numbers Commented May 30, 2015 at 11:16

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help but the question was to be solved on an OJ and the ListNode definition could not have been modified. I have solved it , and figured out the mistake. Thanks again
0

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.

4 Comments

i have to return the top of the linked list which contains the sum. that's why I'm not updating top.
Yeah that code is anyways not supposed to be executed . X is just used in the upper while loop to update the first node.
That code is one which will add the add [1]. If that s not executed then you will get only [0] (as you are getting now). If it is executed you will get [0]->[1] (as required). I suggest you reconsider your logic (and what you want to achieve using the code)
if(x==0){top.next = sum; x++;} <== this is not supposed to be executed. The rest i suppose is correct. You;re not being helpful at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.