Leetcode——445. 两数相加 II

在这里插入图片描述
思路:利用栈来解决链表反转
简洁版

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 carry = 0;
        ListNode* ans = nullptr;
        while (!s1.empty() or !s2.empty() or carry != 0) {
            int a = s1.empty() ? 0 : s1.top();
            int b = s2.empty() ? 0 : s2.top();
            if (!s1.empty()) s1.pop();
            if (!s2.empty()) s2.pop();
            int cur = a + b + carry;
            carry = cur / 10;
            cur %= 10;
            auto curnode = new ListNode(cur);
            curnode -> next = ans;
            ans = curnode;
        }
        return ans;
    }
};

复杂版

/**
 * 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;
        while(l1!=NULL)
        {
            s1.push(l1->val);
            l1=l1->next;
        }
        stack<int> s2;
        while(l2!=NULL)
        {
            s2.push(l2->val);
            l2=l2->next;
        }
        int n1=s1.size();
        int n2=s2.size();
        stack<int> s3;
        int tempjin=0;
        if(n1>=n2)
        {
            while(s2.size()!=0)
            {
                int temp1=s1.top();
                s1.pop();
                int temp2=s2.top();
                s2.pop();
                int temp3=(temp1+temp2+tempjin)%10;
                tempjin=(temp1+temp2+tempjin)/10;
                s3.push(temp3);
            }
            while(!s1.empty())
            {
                int temp1=s1.top();
                s1.pop();
                int temp3=(temp1+tempjin)%10;
                tempjin=(tempjin+temp1)/10;
                s3.push(temp3);
            }
            if(s1.empty()&&s2.empty()&&tempjin==1)
            {
                s3.push(1);
            }
            int temp=s3.top();
            s3.pop();
            ListNode* l=new ListNode(temp);
            ListNode* head=l;
            while(s3.size()!=0)
            {
                int temp4=s3.top();
                s3.pop();
                ListNode* l4=new ListNode(temp4);
                l->next=l4;
                l=l4;
            }
            return head;
        }
        else
        {
            while(s1.size()!=0)
            {
                int temp1=s2.top();
                s2.pop();
                int temp2=s1.top();
                s1.pop();
                int temp3=(temp1+temp2+tempjin)%10;
                tempjin=(temp1+temp2+tempjin)/10;
                s3.push(temp3);
            }
            while(!s2.empty())
            {
                int temp1=s2.top();
                s2.pop();
                int temp3=(temp1+tempjin)%10;
                tempjin=(tempjin+temp1)/10;
                s3.push(temp3);
            }
            if(s1.empty()&&s2.empty()&&tempjin==1)
            {
                s3.push(1);
            }
            int temp=s3.top();
            s3.pop();
            ListNode* l=new ListNode(temp);
            ListNode* head=l;
            while(s3.size()!=0)
            {
                int temp4=s3.top();
                s3.pop();
                ListNode* l4=new ListNode(temp4);
                l->next=l4;
                l=l4;
            }
            return head;
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值