
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int sum = 0;
            ListNode head = l1;
            ListNode temp = null;
            while (l1 != null && l2 != null) {
                sum += l1.val + l2.val;
                l1.val = sum % 10;
                sum = sum / 10;
                if (l1.next == null) {
                    temp = l1;
                }
                l1 = l1.next;
                l2 = l2.next;
            }
            while (l1 != null) {
                sum += l1.val;
                l1.val = sum % 10;
                sum = sum / 10;
                if (l1.next == null) {
                    temp = l1;
                }
                l1 = l1.next;
            }
            while (l2 != null && temp != null) {
                sum += l2.val;
                temp.next = new ListNode(sum % 10);
                sum = sum / 10;
                temp = temp.next;
                l2 = l2.next;
            }
            if (sum > 0) {
                temp.next = new ListNode(sum);
            }
            return head;
        }
    }

//runtime:2 ms
//memory:38.9 MB
