[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.html

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

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

Example:git

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便可,參見代碼以下:函數

 

解法一:post

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,最後回到原函數中,咱們仍要處理進位狀況,參見代碼以下:url

 

解法二:spa

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的下一個節點。code

 

解法三:htm

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 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索