leetcode445. Add Two Numbers II

题目描述

You are given two non-empty linked lists representing two non-negative integers. 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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码

利用reverse函数将问题转化为leetcode2.问题

#include <iostream>

using namespace std;


/// Using reverse
/// Time Complexity: O(m + n + max(m, n))
/// Space Complexity: O(1)

/// 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) {

        l1 = reverse(l1);
        l2 = reverse(l2);
        ListNode* dummyHead = new ListNode(-1), *cur = dummyHead;
        int carry = 0;
        for(ListNode* node1 = l1, *node2 = l2; node1 || node2 || carry;
            node1 = node1 ? node1->next : NULL, node2 = node2 ? node2->next : NULL){

            int x = node1 ? node1->val : 0;
            x += node2 ? node2->val : 0;
            x += carry;
            cur->next = new ListNode(x % 10);
            cur = cur->next;
            carry = x / 10;
        }
        return reverse(dummyHead->next);
    }

private:
    ListNode* reverse(ListNode* node){

        if(!node->next) return node;

        ListNode* ret = reverse(node->next);
        node->next->next = node;
        node->next = NULL;
        return ret;
    }
};


int main() {

    return 0;
}

思路二

利用栈

#include <iostream>
#include <stack>

using namespace std;


/// Using Stack
/// Time Complexity: O(m + n + max(m, n))
/// Space Complexity: O(m + n)

/// 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<ListNode*> stack1, stack2, stack;
        ListNode* node = l1;
        while(node) stack1.push(node), node = node->next;
        node = l2;
        while(node) stack2.push(node), node = node->next;

        int carry = 0;
        while(!stack1.empty() || !stack2.empty() || carry){

            int x = 0;
            if(!stack1.empty()) x += stack1.top()->val, stack1.pop();
            if(!stack2.empty()) x += stack2.top()->val, stack2.pop();
            x += carry;

            stack.push(new ListNode(x % 10));
            carry = x / 10;
        }

        ListNode* ret = stack.top(), *cur = ret;
        stack.pop();
        while(!stack.empty())
            cur->next = stack.top(), cur = cur->next, stack.pop();
        return ret;
    }
};


int main() {

    return 0;
}

思路三

有待学习

#include <iostream>
#include <stack>

using namespace std;


/// Recursion
/// Time Complexity: O(m + n + max(m, n))
/// Space Complexity: O(max(m, n))

/// 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 len1 = get_length(l1), len2 = get_length(l2);
        int len = max(len1, len2);

        if(len1 != len2){
            ListNode* head = len1 < len2 ? l1 : l2;
            for(int i = 0; i < len - min(len1, len2); i ++){
                ListNode* node = new ListNode(0);
                node->next = head;
                head = node;
            }
            if(len1 < len2) l1 = head;
            else l2 = head;
        }

        int carry = 0;
        ListNode* res = go(l1, l2, carry);
        if(carry){
            ListNode* node = new ListNode(1);
            node->next = res;
            res = node;
        }
        return res;
    }

private:
    int get_length(ListNode* head){

        if(!head) return 0;
        return 1 + get_length(head->next);
    }

    ListNode* go(ListNode* l1, ListNode* l2, int& carry){

        if(!l1){
            assert(!l2);
            carry = 0;
            return NULL;
        }

        int c = 0;
        ListNode* next = go(l1->next, l2->next, c);
        int x = l1->val + l2->val + c;
        ListNode* res = new ListNode(x % 10);
        res->next = next;
        carry = x / 10;
        return res;
    }
};


int main() {

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值