[LeetCode]Add Two Numbers
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry = 0; ListNode result = new ListNode(0); ListNode p = result; while (l1 != null && l2 != null) { int tmp = l1.val + l2.val + carry; carry = tmp > 9 ? 1 : 0; tmp = tmp > 9 ? tmp - 10 : tmp; p.next = new ListNode(tmp); p = p.next; l1 = l1.next; l2 = l2.next; } if (l1 == null && l2 == null) { if (carry != 0) p.next = new ListNode(carry); } else if (carry == 0) { p.next = l1 == null ? l2 : l1; } else { ListNode l = l1 == null ? l2 : l1; p.next = addTwoNumbers(l, new ListNode(carry)); } return result.next; } }