What is happening here? Leetcode saves my static var from previous run? Please help.


  • 0
    F

    I used a private static variable in my case. when I run my code for [9] and [9] case it worked and returned 8 -> 1. But leedcode reurned my code with 9 -> 1. Did it save the integer prev from my previous case?
    0_1476940674307_Capture.JPG

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode res = new ListNode(prev);
            if(l1 == null && l2 == null && prev == 0) 
                return null;
            else if(l1 == null && l2 == null && prev != 0)
                return res;
            else if(l2 == null || l1 == null){
                res = (l1 == null? l2 : l1);
                res.val = (l1 == null? l2.val + prev: l1.val + prev) % 10;
                prev = (l1 == null? l2.val + prev: l1.val + prev) / 10;
                res.next = addTwoNumbers((l1 == null? l2.next : l1.next),null);
                return res;
            }
            res.val = (l1.val + l2. val + prev)%10;
            prev = (l1.val + l2.val + prev)/10;
            res.next = addTwoNumbers(l1.next, l2.next);
            return res;
        }
        private static int prev = 0;
    }
    

Log in to reply
 

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