Why I can't get pass


  • 0
    R

    Hi everybody, I can't pass the test case below. Actually I've changed the aNode list into [0,1], but why it can't show all of them. I am very confusing! Please help me fix it out, thanks so much!

    public class Solution {
    
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {   
        	ListNode aNode = l1;
        	ListNode bNode = l2;
        	ListNode recNode = aNode;
            int carry = 0;
    	    int cur_carry = 0;
            int count = 0;
        	
        	while (aNode != null || bNode != null || carry != 0) {
        		if (aNode == null) aNode = new ListNode(0);
        		if (bNode == null) bNode = new ListNode(0);
        		
        		cur_carry = (carry + aNode.val + bNode.val) / 10;
        		aNode.val = (carry + aNode.val + bNode.val) % 10;
                
                count ++;
                System.out.println("count:"+count+" aNode.val:"+aNode.val);
                
                aNode = aNode.next;
                bNode = bNode.next;
                carry = cur_carry;
                
                
        	}
            	
        	return recNode;
        }
    }    
    
    Input:
    [5]
    [5]
    Output:
    [0]
    Expected:
    [0,1]
    Stdout:
    count:1 aNode.val:0
    count:2 aNode.val:1

  • 0
    Y

    the problem is you lost the link from Node 0 to Node 1.


  • 0
    R
    This post is deleted!

  • 0
    R

    @yangling1985 said in Why I can't get pass:

    the problem is you lost the link from Node 0 to Node 1.

    can you explain it more clearly?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.