A simple recursive solution in Java


  • 0
    private ListNode addTwoNumbersRec(int carry, ListNode l1, ListNode l2) {
        if (l1 == null && l2 == null && carry == 0)
            return null;
        
        int l1Val = l1 != null ? l1.val : 0;
        int l2Val = l2 != null ? l2.val : 0;
        ListNode l1Next = l1 != null ? l1.next : null;
        ListNode l2Next = l2 != null ? l2.next : null;
        
        int sum = l1Val + l2Val + carry;
        int val = sum % 10;
        int newCarry = sum / 10;
        
        ListNode newNode = new ListNode(val);
        newNode.next = addTwoNumbersRec(newCarry, l1Next, l2Next);
        
        return newNode;
    }
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        return addTwoNumbersRec(0,l1,l2);
    }

Log in to reply
 

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