leetcode 445. 两数相加2

官方题解

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;
    }
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/add-two-numbers-ii/solution/liang-shu-xiang-jia-ii-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

我的解法

  1. 遍历链表,把各位数字压栈
  2. 依次取栈顶元素,相加,创建链表节点,加入链表
  3. 返回新链表头指针
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    stack<int> getNum(ListNode* head) {
        stack<int> num;
        while(head != NULL) {
            num.push(head->val);
            head = head->next;
        }
        return num;
    }
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 
        if(l1->val == 0) return l2;
        if(l2->val == 0) return l1;
        stack<int> num1 = getNum(l1);
        stack<int> num2 = getNum(l2);
        ListNode* ptr = NULL;
        ListNode* curr;
        int carry = 0;
        while(!num1.empty() || !num2.empty()) {
            int sum;
            if(!num1.empty() && !num2.empty()) {
                sum = num1.top() + num2.top();
                num1.pop();
                num2.pop();
            }
            else if(!num1.empty()) {
                sum = num1.top();
                num1.pop();
            }
            else if(!num2.empty()) {
                sum = num2.top();
                num2.pop();
            }
            //比如99 + 7
            //sum % 10 + carry有可能大于9!!
            //curr = new ListNode(sum % 10 + carry); //错误写法
            int actual_sum = sum + carry;
            curr = new ListNode(actual_sum % 10);
            carry = actual_sum / 10;
            curr->next = ptr;
            ptr = curr;
        }
        //处理例如 5 + 5的情况,不然就丢掉了进位的1
        if(carry != 0) {
            curr = new ListNode(carry);
            curr->next = ptr;
            ptr = curr;            
        }
        return ptr;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值