@Test public void addTwoNumbersTest2() { final ListNode l1 = ListNode.make(new int[] {0}); final ListNode l2 = ListNode.make(new int[] {0}); final ListNode result = problem.addTwoNumbers(l1, l2); Assert.assertTrue(ListNode.compare(result, new int[] {0})); }
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode hdr1 = l1; ListNode hdr2 = l2; ListNode hdr = null; ListNode tail = null; int carry = 0; while (!(hdr1 == null && hdr2 == null)) { final int val1 = hdr1 == null ? 0 : hdr1.val; final int val2 = hdr2 == null ? 0 : hdr2.val; hdr1 = hdr1 == null ? null : hdr1.next; hdr2 = hdr2 == null ? null : hdr2.next; final int num = val1 + val2 + carry; final int val = num % 10; carry = val != num ? 1 : 0; final ListNode node = new ListNode(val); if (hdr == null) { hdr = node; tail = node; } else { tail.next = node; tail = node; } } if (carry != 0) { final ListNode node = new ListNode(1); if (hdr == null) { hdr = node; } else { tail.next = node; } } return hdr == null ? new ListNode(0) : hdr; }
Problem0002AddTwoNumbers { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode hdr1 = l1; ListNode hdr2 = l2; ListNode hdr = null; ListNode tail = null; int carry = 0; while (!(hdr1 == null && hdr2 == null)) { final int val1 = hdr1 == null ? 0 : hdr1.val; final int val2 = hdr2 == null ? 0 : hdr2.val; hdr1 = hdr1 == null ? null : hdr1.next; hdr2 = hdr2 == null ? null : hdr2.next; final int num = val1 + val2 + carry; final int val = num % 10; carry = val != num ? 1 : 0; final ListNode node = new ListNode(val); if (hdr == null) { hdr = node; tail = node; } else { tail.next = node; tail = node; } } if (carry != 0) { final ListNode node = new ListNode(1); if (hdr == null) { hdr = node; } else { tail.next = node; } } return hdr == null ? new ListNode(0) : hdr; } }
Problem0002AddTwoNumbers { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode hdr1 = l1; ListNode hdr2 = l2; ListNode hdr = null; ListNode tail = null; int carry = 0; while (!(hdr1 == null && hdr2 == null)) { final int val1 = hdr1 == null ? 0 : hdr1.val; final int val2 = hdr2 == null ? 0 : hdr2.val; hdr1 = hdr1 == null ? null : hdr1.next; hdr2 = hdr2 == null ? null : hdr2.next; final int num = val1 + val2 + carry; final int val = num % 10; carry = val != num ? 1 : 0; final ListNode node = new ListNode(val); if (hdr == null) { hdr = node; tail = node; } else { tail.next = node; tail = node; } } if (carry != 0) { final ListNode node = new ListNode(1); if (hdr == null) { hdr = node; } else { tail.next = node; } } return hdr == null ? new ListNode(0) : hdr; }  }
Problem0002AddTwoNumbers { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode hdr1 = l1; ListNode hdr2 = l2; ListNode hdr = null; ListNode tail = null; int carry = 0; while (!(hdr1 == null && hdr2 == null)) { final int val1 = hdr1 == null ? 0 : hdr1.val; final int val2 = hdr2 == null ? 0 : hdr2.val; hdr1 = hdr1 == null ? null : hdr1.next; hdr2 = hdr2 == null ? null : hdr2.next; final int num = val1 + val2 + carry; final int val = num % 10; carry = val != num ? 1 : 0; final ListNode node = new ListNode(val); if (hdr == null) { hdr = node; tail = node; } else { tail.next = node; tail = node; } } if (carry != 0) { final ListNode node = new ListNode(1); if (hdr == null) { hdr = node; } else { tail.next = node; } } return hdr == null ? new ListNode(0) : hdr; }  ListNode addTwoNumbers(ListNode l1, ListNode l2); }
Problem0002AddTwoNumbers { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode hdr1 = l1; ListNode hdr2 = l2; ListNode hdr = null; ListNode tail = null; int carry = 0; while (!(hdr1 == null && hdr2 == null)) { final int val1 = hdr1 == null ? 0 : hdr1.val; final int val2 = hdr2 == null ? 0 : hdr2.val; hdr1 = hdr1 == null ? null : hdr1.next; hdr2 = hdr2 == null ? null : hdr2.next; final int num = val1 + val2 + carry; final int val = num % 10; carry = val != num ? 1 : 0; final ListNode node = new ListNode(val); if (hdr == null) { hdr = node; tail = node; } else { tail.next = node; tail = node; } } if (carry != 0) { final ListNode node = new ListNode(1); if (hdr == null) { hdr = node; } else { tail.next = node; } } return hdr == null ? new ListNode(0) : hdr; }  ListNode addTwoNumbers(ListNode l1, ListNode l2);  }