/** * ãå·¥ç¨ã: LeetCode å å: PACKAGE_NAME ç±»å: Add Two Numbers * ãä½è ã: liuyang * ãæ¶é´ã: 16/10/6 ä¸å9:48 * ãé¢ç®ã: Add Two Numbers * ãå 容ã: * ãçæ¬ã: V1.0 * ãè¿è¡æ¶é´ã: * ãæ¶é´å¤æåº¦ã: * ã空é´å¤æåº¦ã: * ã夿³¨ã: * ãæè·¯ã: */ public class AddTwoNumbers { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode head = new ListNode(0); ListNode p = l1, q = l2, cur = head; int carry = 0; while (p != null || q != null) { int x = (p != null) ? p.val : 0; int y = (q != null) ? q.val : 0; int sum = x + y + carry; carry = sum / 10; cur.next = new ListNode(sum%10); cur = cur.next; if (p != null) p = p.next; if (q != null) q = q.next; } if (carry == 1) { cur.next = new ListNode(carry); } return head.next; } } class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }