My java solution


  • 0
    S
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    int carry = 0;
    ListNode head = new ListNode(0);
    ListNode d = head;
    ArrayList <ListNode> n = new ArrayList <ListNode>();
    while(l1 != null || l2!=null || carry!=0){
    if(l1 == null){
    l1 = new ListNode(0);
    }
    if(l2 == null){
    l2 = new ListNode(0);
    }
    int i = l1.val + l2.val + carry;
    if (i >= 10){
    i = i - 10;
    carry = 1;
    }else{
    carry = 0;
    }
    d.next = new ListNode(i);
    d = d.next;
    l1 = l1.next;
    l2 = l2.next;
    }
        return head.next;
    }
    
    

Log in to reply
 

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