@@ -0,0 +1,2018 @@
+/***%0A * You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.%0A *%0A * Input: (2 -%3E 4 -%3E 3) + (5 -%3E 6 -%3E 4)%0A * Output: 7 -%3E 0 -%3E 8%0A */%0A/**%0A * Definition for singly-linked list.%0A * public class ListNode %7B%0A *     int val;%0A *     ListNode next;%0A *     ListNode(int x) %7B%0A *         val = x;%0A *         next = null;%0A *     %7D%0A * %7D%0A*/%0Apublic class Solution %7B%0A    // Solution 1: O(m + n), O(1)%0A    /*%0A    public ListNode addTwoNumbers(ListNode l1, ListNode l2) %7B%0A        ListNode ret = new ListNode(-1);%0A        ListNode prev = ret;%0A        int ai, bi, val;%0A        int carry = 0;%0A        for (ListNode pa = l1, pb = l2; pa != null %7C%7C pb != null; %0A                pa = pa == null ? null : pa.next,%0A                pb = pb == null ? null : pb.next,%0A                prev = prev.next) %7B%0A            ai = pa == null ? 0 : pa.val;%0A            bi = pb == null ? 0 : pb.val;%0A            val = (ai + bi + carry) %25 10;%0A            carry = (ai + bi + carry) / 10;%0A            prev.next = new ListNode(val);%0A                %7D%0A        if (carry %3E 0)%0A            prev.next = new ListNode(carry);%0A        return ret.next;%0A    %7D%0A    */%0A%0A    // Solution 2: %0A    public ListNode addTwoNumbers(ListNode l1, ListNode l2) %7B%0A        ListNode ret = new ListNode(-1);%0A        ListNode cur = ret;%0A        int addup = 0;%0A        while (l1 != null %7C%7C l2 != null) %7B%0A            if (l1 != null && l2 != null )%0A                sum = l1.val + l2.val + addup;%0A            else if (l1 != null)%0A                sum = l1.val + addup;%0A            else %0A                sum = l2.val + addup;%0A            cur.next = new ListNode(sum %25 10);%0A            addup = sum / 10 == 1 ? 1 : 0;%0A            l1 = l1 == null ? null : l1.next;%0A            l2 = l2 == null ? null : l2.next;%0A            cur = cur.next;%0A        %7D%0A        if (addup == 1)%0A            cur.next = new ListNode(1);%0A%0A        return ret.next;%0A    %7D%0A%7D%0A

add AddTwoNumbers.java