欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Leetcode——445. 两数相加 II

程序员文章站 2022-06-07 09:58:14
...

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;
        }
    }
};