[leetcode]2-Add Two Numbers

2. Add Two Numbers

1)題目

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.

Example:

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

2)思路

其實就是簡單的加法。
第一位就是各位,往上累加便可。node

3) 代碼

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
                ListNode result = new ListNode(-1);
        ListNode p = result;
        ListNode a = l1;
        ListNode b = l2;
        int flag = 0;
        while (a!=null || b!= null) {
            int add1 = a == null ? 0 : a.val;
            int add2 = b == null ? 0 : b.val;
            int sum = add1 + add2 + flag;
            flag = sum >= 10? 1:0;
            int val = sum%10 ;
            ListNode temp = new ListNode(val);
            p.next = temp;
            p = temp;
            if (a != null) {
                a = a.next;
            }
            if (b != null) {
                b = b.next;
            }
        }
        if (flag ==1) {
            p.next = new ListNode(1);
        }
        return result.next;

    }
}

4) 結果

時間複雜度:O(n)
空間複雜度:O(1)
耗時:52 msgit

5) 調優

看了下時間少的算法,基本邏輯是一致的。 我運行了一遍 耗時和這差很少。
官網可能因爲啥偶然因素致使他的代碼畢竟快吧。算法

相關文章
相關標籤/搜索