[LeetCode] Add Two Numbers II 兩個數字相加之二


 

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

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

 

這道題是之前那道Add Two Numbers的拓展,我們可以看到這道題的最高位在鏈表首位置,如果我們給鏈表翻轉一下的話就跟之前的題目一樣了,這里我們來看一些不修改鏈表順序的方法。由於加法需要從最低位開始運算,而最低位在鏈表末尾,鏈表只能從前往后遍歷,沒法取到前面的元素,那怎么辦呢?我們可以利用棧來保存所有的元素,然后利用棧的后進先出的特點就可以從后往前取數字了,我們首先遍歷兩個鏈表,將所有數字分別壓入兩個棧s1和s2中,我們建立一個值為0的res節點,然后開始循環,如果棧不為空,則將棧頂數字加入sum中,然后將res節點值賦為sum%10,然后新建一個進位節點head,賦值為sum/10,如果沒有進位,那么就是0,然后我們head后面連上res,將res指向head,這樣循環退出后,我們只要看res的值是否為0,為0返回res->next,不為0則返回res即可,參見代碼如下:

 

解法一:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        stack<int> s1, s2;
        while (l1) {
            s1.push(l1->val);
            l1 = l1->next;
        }
        while (l2) {
            s2.push(l2->val);
            l2 = l2->next;
        }
        int sum = 0;
        ListNode *res = new ListNode(0);
        while (!s1.empty() || !s2.empty()) {
            if (!s1.empty()) {sum += s1.top(); s1.pop();}
            if (!s2.empty()) {sum += s2.top(); s2.pop();}
            res->val = sum % 10;
            ListNode *head = new ListNode(sum / 10);
            head->next = res;
            res = head;
            sum /= 10;
        }
        return res->val == 0 ? res->next : res;
    }
};

 

下面這種方法使用遞歸來實現的,我們知道遞歸其實也是用棧來保存每一個狀態,那么也就可以實現從后往前取數字,我們首先統計出兩個鏈表長度,然后根據長度來調用遞歸函數,需要傳一個參數差值,遞歸函數參數中的l1鏈表長度長於l2,在遞歸函數中,我們建立一個節點res,如果差值不為0,節點值為l1的值,如果為0,那么就是l1和l2的和,然后在根據差值分別調用遞歸函數求出節點post,然后要處理進位,如果post的值大於9,那么對10取余,且res的值自增1,然后把pos連到res后面,返回res,最后回到原函數中,我們仍要處理進位情況,參見代碼如下:

 

解法二:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int n1 = getLength(l1), n2 = getLength(l2);
        ListNode *head = new ListNode(1);
        head->next = (n1 > n2) ? helper(l1, l2, n1 - n2) : helper(l2, l1, n2 - n1);
        if (head->next->val > 9) {
            head->next->val %= 10;
            return head;
        }
        return head->next;
    }
    int getLength(ListNode* head) {
        int cnt = 0;
        while (head) {
            ++cnt;
            head = head->next;
        }
        return cnt;
    }
    ListNode* helper(ListNode* l1, ListNode* l2, int diff) {
        if (!l1) return NULL;
        ListNode *res = (diff == 0) ? new ListNode(l1->val + l2->val) : new ListNode(l1->val);
        ListNode *post = (diff == 0) ? helper(l1->next, l2->next, 0) : helper(l1->next, l2, diff - 1);
        if (post && post->val > 9) {
            post->val %= 10;
            ++res->val;
        }
        res->next = post;
        return res;
    }
};

 

下面這種方法借鑒了Plus One Linked List中的解法三,在處理加1問題時,我們需要找出右起第一個不等於9的位置,然后此位置值自增1,之后的全部賦為0。這里我們同樣要先算出兩個鏈表的長度,我們把其中較長的放在l1,然后我們算出兩個鏈表長度差diff。如果diff大於0,我們用l1的值新建節點,並連在cur節點后(cur節點初始化時指向dummy節點)。並且如果l1的值不等於9,那么right節點也指向這個新建的節點,然后cur和l1都分別后移一位,diff自減1。當diff為0后,我們循環遍歷,將此時l1和l2的值加起來放入變量val中,如果val大於9,那么val對10取余,right節點自增1,將right后面節點全賦值為0。在cur節點后新建節點,節點值為更新后的val,如果val的值不等於9,那么right節點也指向這個新建的節點,然后cur,l1和l2都分別后移一位。最后我們看dummy節點值若為1,返回dummy節點,如果是0,則返回dummy的下一個節點。

 

解法三:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int n1 = getLength(l1), n2 = getLength(l2), diff = abs(n1 - n2);
        if (n1 < n2) swap(l1, l2);
        ListNode *dummy = new ListNode(0), *cur = dummy, *right = cur;
        while (diff > 0) {
            cur->next = new ListNode(l1->val);
            if (l1->val != 9) right = cur->next;
            cur = cur->next;
            l1 = l1->next;
            --diff;
        }
        while (l1) {
            int val = l1->val + l2->val;
            if (val > 9) {
                val %= 10;
                ++right->val;
                while (right->next) {
                    right->next->val = 0;
                    right = right->next;
                }
                right = cur;
            }
            cur->next = new ListNode(val);
            if (val != 9) right = cur->next;
            cur = cur->next;
            l1 = l1->next;
            l2 = l2->next;
        }
        return (dummy->val == 1) ? dummy : dummy->next;
    }
    int getLength(ListNode* head) {
        int cnt = 0;
        while (head) {
            ++cnt;
            head = head->next;
        }
        return cnt;
    }
};

 

類似題目:

Add Two Numbers

Plus One Linked List 

 

參考資料:

https://discuss.leetcode.com/topic/67076/ac-follow-up-java

https://discuss.leetcode.com/topic/65279/easy-o-n-java-solution-using-stack

https://discuss.leetcode.com/topic/65306/java-o-n-recursive-solution-by-counting-the-difference-of-length/2

https://discuss.leetcode.com/topic/66699/java-iterative-o-1-space-lastnot9-solution-changed-from-plus-one-linked-list

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM