Why Runtime Error with running in eclipse well?


  • 0
    Z

    I'd like to use recursion instead of while. ty.

    public class Solution {

    int add = 0;
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    
        if(l1 == null && l2 == null && add == 0){
    
            return null;
    
        }else if(l1 == null && l2 == null){
    
            return new ListNode(add);
    
        }else if(l1 == null){
    
            ListNode result = new ListNode((l2.val + add) % 10);
    
            add = (l2.val + add) / 10;
    
            result.next = addTwoNumbers(l1, l2.next);
    
            return result;
    
        }else if(l2 == null){
    
            ListNode result = new ListNode((l1.val + add) % 10);
    
            add = (l1.val + add) / 10;
    
            result.next = addTwoNumbers(l1.next, l2);
    
            return result;
    
        }else{
    
            ListNode result = new ListNode((l1.val + l2.val + add) % 10);
    
            add = (l1.val + l2.val + add) / 10;
    
            result.next = addTwoNumbers(l1.next, l2.next);
    
            return result;
    
        } 
    }
    

    }


Log in to reply
 

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