Is there something wrong with Java submissions' runtime?


  • 1
    L

    For all the 3 to 5 ms (who declaimed) Java submissions, I have ran several times, all around 50 ms.

    I'm confused, help! And below is my 54 ms Java code.

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if (l1 == null && l2 == null) return null;
            
            int carry = 0;
            ListNode prev = new ListNode(-1), head = prev;
            while (l1 != null || l2 != null || carry != 0) {
                int sum = (l1!=null?l1.val:0)+(l2!=null?l2.val:0)+carry;
                prev.next = new ListNode(sum%10);
                carry = sum/10;
                prev = prev.next;
                if (l1 != null) l1 = l1.next; 
                if (l2 != null) 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.