Add Two Numbers Leetcode

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) {
        boolean flag = false;
        ListNode res = new ListNode(0);
        ListNode head = res;
        while (l1 != null && l2 != null) {
            int digit = l1.val + l2.val;
            ListNode d = null;
            if (digit > 9) {
                d = flag ? new ListNode(digit % 10 + 1) : new ListNode(digit % 10);
                flag = true;
            } else if (flag) {
                if (digit + 1 > 9) {
                    d = new ListNode((digit + 1) % 10);
                    flag = true;
                } else {
                    d = new ListNode(digit + 1);
                    flag = false;
                }
            } else {
                d = new ListNode(digit);
                flag = false;
            }
            res.next = d;
            res = d;
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null) {
            int digit = 0;
            if (flag) {
                digit = l1.val + 1;
                if (digit > 9) {
                    flag = true;
                    digit = digit % 10;
                } else {
                    flag = false;
                }
            } else {
                digit = l1.val;
                flag = false;
            }
            ListNode d = new ListNode(digit);
            res.next = d;
            res = d;
            l1 = l1.next;
        }
        while (l2 != null) {
            int digit = 0;
            if (flag) {
                digit = l2.val + 1;
                if (digit > 9) {
                    flag = true;
                    digit = digit % 10;
                } else {
                    flag = false;
                }
            } else {
                digit = l2.val;
                flag = false;
            }
            ListNode d = new ListNode(digit);
            res.next = d;
            res = d;
            l2 = l2.next;
        }
        if (flag) {
            ListNode d = new ListNode(1);
            res.next = d;
        }
        return head.next;
    }
}

我是把相同的位数相加,然后再判断剩下那个比较长的数字。

/**
 * 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) {
        ListNode res = new ListNode(0);
        ListNode p = res;
        int sum = 0;
        while (l1 != null || l2 != null) {
            if (l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            p.next = new ListNode(sum % 10);
            p = p.next;
            sum /= 10;
        }
        if (sum == 1) {
            p.next = new ListNode(1);
        }
        return res.next;
    }
}

反正一对比就高下立现。。。嘤嘤嘤。。。

时隔这么久再写了一次还是不忍直视。。。思路就是有一个sum作为变量,如果两个都不为null就还可以加完一个再加一个,超过十就进位。加的是除以10的余数。如果有一个为null就一直加到最后,别忘记最后要进1.

到时候回顾一下吧。。。

 

posted @ 2017-01-25 11:02  璨璨要好好学习  阅读(154)  评论(0编辑  收藏  举报