2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

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

        while (l1 != null) {
            head.next = new ListNode((l1.val + carry) % 10);
            carry = (l1.val + carry) / 10;
            l1 = l1.next;
            head = head.next;
        }

        while (l2 != null) {
            head.next = new ListNode((l2.val + carry) % 10);
            carry = (l2.val + carry) / 10;
            l2 = l2.next;
            head = head.next;
        }

        if (carry == 1) {
            head.next = new ListNode(1);
        }
        return dummy.next;

        /**
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode dummy = new ListNode(0);
        ListNode head = dummy;
        int carry = 0;

        while (l1 != null && l2 != null) { // Should be l1/l2, not l1/l2.next
            head.next = new ListNode((l1.val + l2.val + carry) % 10);
            carry = (l1.val + l2.val + carry) /10;
            head = head.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        if (carry == 1) {
            if (l1 == null && l2 == null) {
                head.next = new ListNode(1);
            } else if (l1 == null) {
                while (l2 != null) {
                    head.next = new ListNode((l2.val + carry) % 10);
                    carry = (l2.val + carry) / 10;
                    head = head.next; // Also don't forget this.
                    l2 = l2.next; // Don't forget this.
                }
            } else if (l2 == null) {
                while (l1 != null) {
                    head.next = new ListNode((l1.val + carry) % 10);
                    carry = (l1.val + carry) / 10;
                    head = head.next; // Don't forget.
                    l1 = l1.next; // Don't forget this.
                }
            }
            if (carry == 1) {
                head.next = new ListNode(1);
            }
            return dummy.next;
        }
        if (l1 == null && l2 != null) {
            head.next = l2;
        }
        if (l2 == null && l1 != null) {
            head.next = l1;
        }
        return dummy.next;
    */
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s