Use java complete the issue.


  • 0
    O
    public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    	ListNode result=new ListNode(0);
    	int jinwei=0;
    	ListNode tempNode=result;
    	while(l1!=null||l2!=null){
    		
    		int temp=(l1==null)?(jinwei+l2.val):(l2==null?jinwei+l1.val:jinwei+l2.val+l1.val);
    
    		jinwei=temp/10;
    		tempNode.val=temp%10;
    		
    		if(l1!=null){
    			l1=l1.next;
    		}
    		if(l2!=null){
    			l2=l2.next;
    		}
    		if(l1!=null||l2!=null){
    			tempNode.next=new ListNode(0);
    			tempNode=tempNode.next;
    		}else if(jinwei==1){
    			tempNode.next=new ListNode(1);
    			tempNode=tempNode.next;
    		}
    	}
    	return result;
    }
    

    }


Log in to reply
 

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