[每日一题] 118. 两数相加 II(链表、数学、多方法)

1. 题目来源

链接:两数相加 II
来源:LeetCode

2. 题目说明

给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

进阶:

如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。

示例1:

输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7

3. 题目解析

方法一:翻转链表法

跟前一道两数相加有所不同的是,该题把链表存的数字方向变了,原来是表头存最低位,现在是表头存最高位。

既然是翻转了链表,那么一种直接的解法是把两个输入链表都各自翻转一下,然后用之前的方法相加完成,再把得到的结果翻转一次,就是结果了,翻转链表的方法比较简单,习题可以在LeetCode 206. 反转链表中进行练习。代码如下:

// 执行用时 :32 ms, 在所有 C++ 提交中击败了55.83%的用户
// 内存消耗 :10.7 MB, 在所有 C++ 提交中击败了74.35%的用户

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode* tmp = new ListNode(-1), *cur = tmp;
        int cnt = 0;
        l1 = reverseList(l1);
        l2 = reverseList(l2);
        while (l1 || l2) {
            int val1 = l1 ? l1 -> val : 0;
            int val2 = l2 ? l2 -> val : 0;
            int sum = val1 + val2 + cnt;
            cnt = sum / 10;
            cur -> next = new ListNode(sum % 10);
            cur = cur -> next;
            if (l1) l1 = l1 -> next;
            if (l2) l2 = l2 -> next;
        }
        if (cnt) cur -> next = new ListNode(1);
        return reverseList(tmp -> next);
    }

    ListNode* reverseList(ListNode *head) {
        if (!head) return head;
        ListNode* dummy = new ListNode(-1);
        dummy -> next = head;
        ListNode* cur = head;
        while (cur -> next) {
            ListNode *tmp = cur -> next;
            cur -> next = tmp -> next;
            tmp -> next = dummy -> next;
            dummy -> next = tmp;
        }
        return dummy -> next;
    }
};
方法二:等长补0法、递归解法

采用这种方法,有以下几个要点:

  • 首先要先分别计算出两个链表的长度,然后给稍短一点的链表前面补0,补到和另一个链表相同的长度
  • 由于要从低位开始相加,而低位是链表的末尾,所以采用递归来处理,先遍历到链表的末尾,然后从后面相加,进位标示符carry用的是引用,这样保证了再递归回溯时值可以正确传递,每次计算的节点后面接上上一次回溯的节点,直到回到首节点完成递归。最后还是处理最高位的进位问题。思想不复杂,很朴素的想法,分为各个部分各个实现即可,代码如下:
// 执行用时 :28 ms, 在所有 C++ 提交中击败了75.83%的用户
// 内存消耗 :10.8 MB, 在所有 C++ 提交中击败了71.63%的用户

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int n1 = 0, n2 = 0, carry = 0;;
        n1 = getLength(l1);
        n2 = getLength(l2);
        if (n1 > n2) l2 = padList(l2, n1 - n2);
        if (n2 > n1) l1 = padList(l1, n2 - n1);
        ListNode* res = addTwoNumbersDFS(l1, l2, carry);
        if (carry == 1) {
            ListNode* tmp = new ListNode(1);
            tmp -> next = res;
            res = tmp;
        }
        return res;
    }
    ListNode* addTwoNumbersDFS(ListNode* l1, ListNode* l2, int &carry) {
        if (!l1 && !l2) return NULL;
        ListNode* list = addTwoNumbersDFS(l1 -> next, l2 -> next, carry);
        int sum = l1 -> val + l2 -> val + carry;
        ListNode* res = new ListNode(sum % 10);
        res -> next = list;
        carry = sum / 10;
        return res;
    }
    ListNode* padList(ListNode* list, int len) {
        ListNode* dummy = new ListNode(-1);
        ListNode* cur = dummy;
        for (int i = 0; i < len; ++i) {
            cur -> next = new ListNode(0);
            cur = cur -> next;
        }
        cur -> next = list;
        return dummy -> next;
    }
    int getLength(ListNode* list) {
        ListNode* cur = list;
        int res = 0;
        while (cur) {
            ++res;
            cur = cur -> next;
        }
        return res;
    }
};
方法三:递归解法进阶版

该方法使用递归来实现,即为方法二的进阶版,递归其实也是用栈来保存每一个状态,那么也就可以实现从后往前取数字

  • 首先统计两个链表长度,然后根据长度来调用递归函数,需要传一个参数差值递归函数参数中的l1链表长度长于l2
  • 在递归函数中,建立一个节点res,如果差值不为0,节点值为l1的值,如果为0,那么就是l1和l2的和
  • 再根据差值分别调用递归函数求出节点post
  • 然后要处理进位,如果post的值大于9,那么对10取余,且res的值自增1,然后把post连到res后面,返回res,最后回到原函数中,仍要处理进位情况,参见代码如下:
// 执行用时 :24 ms, 在所有 C++ 提交中击败了88.54%的用户
// 内存消耗 :11.4 MB, 在所有 C++ 提交中击败了61.79%的用户

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
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;
    }
};
方法四:利用栈特性解法吗,好理解!

这里来看一些不修改链表顺序的方法。由于加法需要从最低位开始运算,而最低位在链表末尾,链表只能从前往后遍历,没法取到前面的元素,那怎么办呢?可以利用栈来保存所有的元素,然后利用栈的后进先出的特点就可以从后往前取数字了

  • 首先遍历两个链表,将所有数字分别压入两个栈s1和s2中
  • 建立一个值为0的res节点,开始循环,如果栈不为空,则将栈顶数字加入sum中
  • 然后将res节点值赋为sum %10,并新建一个进位节点head,赋值为 sum / 10,如果没有进位,那么就是0
  • 然后head后面连上res,将res指向head,这样循环退出后,只要看res的值是否为0,为0返回 res -> next,不为0则返回res即可,参见代码如下:
// 执行用时 :24 ms, 在所有 C++ 提交中击败了88.54%的用户
// 内存消耗 :13.4 MB, 在所有 C++ 提交中击败了14.25%的用户

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
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
  • 然后算出两个链表长度差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的下一个节点, 参见代码如下:
// 执行用时 :16 ms, 在所有 C++ 提交中击败了99.27%的用户
// 内存消耗 :10.6 MB, 在所有 C++ 提交中击败了78.89%的用户

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ypuyu

如果帮助到你,可以请作者喝水~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值