Failing a test case, which seems to work fine on paper


  • 0
    A

    The while loop for l2 in the above case should take care of the failing test case.
    Am i missing something very basic here?

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int carry = 0;
            ListNode result = null;
            while (l1 != null && l2 != null) {
                int total = (l1.val + l2.val + carry) % 10;
                carry = (l1.val + l2.val + carry) / 10;
                ListNode r = new ListNode(total);
                if (result == null) {
                    result = r;
                } else {
                    result.next = r;
                }
                l1 = l1.next;
                l2 = l2.next;
            }
            while (l1 != null) {
                int total = (l1.val + carry) % 10;
                carry = (l1.val + carry) / 10;
                ListNode r = new ListNode(total);
                if (result == null) {
                    result = r;
                } else {
                    result.next = r;
                }
                l1 = l1.next;
            }
            while (l2 != null) {
                int total = (l2.val + carry) % 10;
                carry = (l2.val + carry) / 10;
                ListNode r = new ListNode(total);
                if (result == null) {
                    result = r;
                } else {
                    result.next = r;
                }
                l2 = l2.next;
            }
            if (carry == 1) {
                ListNode r = new ListNode(1);
                carry = 0;
                result.next = r;
            }
            return result;
        }
    }
    

    Following is the failing case:
    input : {1},{9,9}
    output : {0,1}
    expected output: {0,0,1}


  • 0
    D

    You forget to advance result. so the code should be as followed. However, u still need to shorten ur code to make it better ^_^

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int carry = 0;
            ListNode result = null;
            ListNode head = new ListNode(0); head.next = result; //dummy head
    
            while (l1 != null && l2 != null) {
                int total = (l1.val + l2.val + carry) % 10;
                carry = (l1.val + l2.val + carry) / 10;
                ListNode r = new ListNode(total);
                if (result == null) {
                    result = r;
                } else {
                    result.next = r;
                }
                l1 = l1.next;
                l2 = l2.next;
                result = r;
            }
            while (l1 != null) {
                int total = (l1.val + carry) % 10;
                carry = (l1.val + carry) / 10;
                ListNode r = new ListNode(total);
                if (result == null) {
                    result = r;
                } else {
                    result.next = r;
                }
                l1 = l1.next;
                result = r; //changed
            }
            while (l2 != null) {
                int total = (l2.val + carry) % 10;
                carry = (l2.val + carry) / 10;
                ListNode r = new ListNode(total);
                if (result == null) {
                    result = r;
                } else {
                    result.next = r;
                }
                l2 = l2.next;
                result = r;
            }
            if (carry == 1) {
                ListNode r = new ListNode(1);
                carry = 0;
                result.next = r;
            }
            return head.next;
        }
    }

  • 0
    A

    @davidtan1890: I realized that after i posted this comment.
    I reduced the code size and it works now :)


Log in to reply
 

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