Wrong answer, but I get right answer in my computer.


  • 0
    W
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int temp = 1;
    	ListNode result = new ListNode(0);
    	ListNode tempNode = new ListNode(0);
    	int a =0;
    	int b =0;
    	while(l1!=null)
    	{
    		a = a+temp*l1.val;
    		temp*=10;
    		l1 = l1.next;
    	}
    	temp =1;
    	while(l2!=null)
    	{
    		b = b+temp*l2.val;
    		temp*=10;
    		l2 = l2.next;
    	}
    	
    	temp = a+b;
    	int len = Integer.toString(temp).length();
    	boolean isfirst = true;
    	for(int i =0;i<len;i++)
    	{
    
    		ListNode curNode = new ListNode(temp%10);
    		temp = temp/10;
    		if(isfirst)
    		{
    			result = curNode;
    			tempNode = result;
    			isfirst = false;
    		}
    		else
    		{
    		tempNode.next = curNode;
    		tempNode = tempNode.next;
    		}
    		
    	}
    	
    
    	return result;
    }

  • 0
    L

    try l1=null and l2=null. The expected result should be null.


Log in to reply
 

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