@@ -0,0 +1,1379 @@
+package com.pengyifan.leetcode;%0A%0Aimport com.pengyifan.leetcode.commons.ListNode;%0A%0A/**%0A * You are given two linked lists representing two non-negative numbers. The%0A * digits are stored in reverse order and each of their nodes contain a single%0A * digit. Add the two numbers and return it as a linked list.%0A * %0A * %3Cpre%3E%0A * Input: (2 -%3E 4 -%3E 3) + (5 -%3E 6 -%3E 4)%0A * Output: 7 -%3E 0 -%3E 8%0A */%0Apublic class AddTwoNumbers %7B%0A%0A  public ListNode addTwoNumbers(ListNode l1, ListNode l2) %7B%0A    if (l1 == null && l2 == null) %7B%0A      return null;%0A    %7D%0A    if (l1 != null && l2 == null) %7B%0A      return l1;%0A    %7D%0A    if (l1 == null && l2 != null) %7B%0A      return l2;%0A    %7D%0A    ListNode head = new ListNode(l1.val + l2.val);%0A    int carry = head.val / 10;%0A    head.val %25= 10;%0A    ListNode cur = head;%0A    l1 = l1.next;%0A    l2 = l2.next;%0A    while (l1 != null && l2 != null) %7B%0A      cur.next = new ListNode(l1.val + l2.val + carry);%0A      cur = cur.next;%0A      carry = cur.val / 10;%0A      cur.val %25= 10;%0A      l1 = l1.next;%0A      l2 = l2.next;%0A    %7D%0A    if (l1 != null) %7B%0A      cur.next = l1;%0A    %7D else %7B%0A      cur.next = l2;%0A    %7D%0A    while (cur.next != null && carry != 0) %7B%0A      cur.next.val += carry;%0A      carry = cur.next.val / 10;%0A      cur.next.val %25= 10;%0A      cur = cur.next;%0A    %7D%0A    if (carry != 0) %7B%0A      // new node%0A      cur.next = new ListNode(carry);%0A    %7D%0A    return head;%0A  %7D%0A%7D%0A

add two numbers