1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class AddTwoNumbers { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } boolean inc = false; int sum = 0; ListNode result = l1; ListNode tmp; if (getLength(l1) >= getLength(l2)) { result = l1; tmp = l2; } else { result = l2; tmp = l1; } ListNode cursor = result; while (tmp != null && tmp.next != null) { sum = result.val + tmp.val + (inc ? 1 : 0); result.val = sum % 10; inc = (sum > 9); result = result.next; tmp = tmp.next; } sum = result.val + tmp.val + (inc ? 1 : 0); result.val = sum % 10; inc = (sum > 9); while (result.next != null) { result = result.next; sum = result.val + (inc ? 1 : 0); result.val = sum % 10; inc = (sum > 9); } if (inc) { result.next = new ListNode(1); } return cursor; } private int getLength(ListNode list) { ListNode cursor = list; int len = 0; while (cursor != null) { len++; cursor = cursor.next; } return len; } }
|