445. 两数相加 II

在这里插入图片描述
两种方法,第一种先反转两条链表然后相加然后再反转
第二种用stack来存,然后对弹出的结点相加即可。最重要的是要让new出来的结点指向前一个结点,而不是前一个结点指向new出来的结点。然后更新前一个结点

/**
 * 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* pre = NULL;
        // ListNode* cur = l1;
        // ListNode* next;
        // while(cur != NULL){
        //     next = cur->next;
        //     cur->next = pre;
        //     pre = cur;
        //     cur = next;
        // }
        // //此时pre就是反转完的链表头
        // ListNode* pre1 = NULL;
        // cur = l2;
        // while(cur != NULL){
        //     next = cur->next;
        //     cur->next = pre1;
        //     pre1 = cur;
        //     cur = next;
        // }
        // int carry = 0, sum = 0;
        // int a = 0, b = 0;
        // ListNode* h = new ListNode(0);
        // cur = h;
        // while(carry != 0 || pre!=NULL || pre1 != NULL){
        //     if(pre == NULL){
        //         a = 0;
        //     }
        //     else{
        //         a = pre->val;
        //         pre = pre->next;
        //     }
        //     if(pre1 == NULL){
        //         b = 0;
        //     }
        //     else{
        //         b = pre1->val;
        //         pre1 = pre1->next;
        //     }
        //     sum = (a+b+carry)%10;
        //     carry = (a+b+carry)/10;
        //     ListNode* node = new ListNode(sum);
        //     cur->next = node;
        //     cur = cur->next;
        // }
        // pre = NULL;
        // cur = h->next;
        // while(cur != NULL){
        //     next = cur->next;
        //     cur->next = pre;
        //     pre = cur;
        //     cur = next;
        // }
        // return pre;
        
        //可以用栈实现的一定可以用递归实现,因为递归会有一个递归栈
        //双栈,依次加入然后依次弹出即可
        stack<ListNode*> sta1;
        stack<ListNode*> sta2;
        while(l1 != NULL){
            sta1.push(l1);
            l1 = l1->next;
        }
        while(l2 != NULL){
            sta2.push(l2);
            l2 = l2->next;
        }
        int sum = 0, carry = 0, a = 0, b = 0;
        ListNode* cur = NULL;
        while(!sta1.empty() || !sta2.empty() || carry != 0){
            if(sta1.empty()){
                a = 0;
            }
            else{
                a = sta1.top()->val;
                sta1.pop();
            }
            if(sta2.empty()){
                b = 0;
            }
            else{
                b = sta2.top()->val;
                sta2.pop();
            }
            sum = (a+b+carry)%10;
            carry = (a+b+carry)/10;
            ListNode* node = new ListNode(sum);
            //生成的结点要倒序输出
            node->next = cur;
            cur = node;
        }
        return cur;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值