LeetCode

LeetCode刷题之Add Two Numbers

2018-04-25  本文已影响0人  JRTx
Problem

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.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
My Solution

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode p = l1, q = l2;
        ListNode res = new ListNode(0);
        ListNode n = res;
        boolean flag = false;
        while (p != null && q != null) {
            int t = 0;
            if (flag == true) {
                t = p.val + q.val + 1;
            } else {
                t = p.val + q.val;
            }
            if (t < 10) {
               n.next = new ListNode(t);
               n = n.next;
               flag = false;
            } else {
               n.next = new ListNode(t % 10);
               n = n.next;
               flag = true;
            }
            p = p.next;
            q = q.next;
        }


        while (p != null) {
            if (flag == true) {
                int t = p.val + 1;
                if (t < 10) {
                   n.next = new ListNode(t);
                   n = n.next;
                   flag = false;
                } else {
                    n.next = new ListNode(t % 10);
                    n = n.next;
                    flag = true;
                }
            } else {
                n.next = new ListNode(p.val);
                n = n.next;
            }
            p = p.next;
        }

        while (q != null) {
            if (flag == true) {
                int t = q.val + 1;
                if (t < 10) {
                    n.next = new ListNode(t);
                    n = n.next;
                    flag = false;
                } else {
                    n.next = new ListNode(t % 10);
                    n = n.next;
                    flag = true;
                }
            } else {
                n.next = new ListNode(q.val);
                n = n.next;
            }
            q = q.next;
        }


        if (p == null && q == null && flag == true) {
            n.next = new ListNode(1);
            n = n.next;
            flag = false;
        } else {

        }
        return res.next;
    }
}
Great Solution

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummyHead = new ListNode(0);
    ListNode p = l1, q = l2, curr = dummyHead;
    int carry = 0;
    while (p != null || q != null) {
        int x = (p != null) ? p.val : 0;
        int y = (q != null) ? q.val : 0;
        int sum = carry + x + y;
        carry = sum / 10;
        curr.next = new ListNode(sum % 10);
        curr = curr.next;
        if (p != null) p = p.next;
        if (q != null) q = q.next;
    }
    if (carry > 0) {
        curr.next = new ListNode(carry);
    }
    return dummyHead.next;
}
上一篇下一篇

猜你喜欢

热点阅读