LeetCode 445两数相加II

LeetCode 445两数相加IIMedium

  • 题目简述:给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。
  • 输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 8 -> 0 -> 7
  • 思路:反转链表后从低位到高位依次相加,最后再反转新求和链表并返回结果
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    //迭代法反转链表
    ListNode* ReverseList(ListNode *head)
    {
        if(head == nullptr || head->next == nullptr) return head;
        ListNode *pre = NULL;
        auto cur = head;
        while(cur)
        {
            auto tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
    //递归法反转链表
    ListNode* ReverseLists(ListNode *head)
    {
        if(head == nullptr || head->next == nullptr) return head;
        ListNode *tail = ReverseLists(head->next);

        head->next->next = head;
        head->next = NULL;
        return tail;
    }
    //反转链表后的计算方式与LeetCode2相同
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        auto rl1 = ReverseLists(l1);
        auto rl2 = ReverseList(l2);
        ListNode *dummy = new ListNode(-1);
        int carry = 0;
        auto cur = dummy;
        while(rl1 || rl2)
        {
            int n1 = rl1 ? rl1->val : 0;
            int n2 = rl2 ? rl2->val : 0;
            int sum = n1 + n2 + carry;
            carry = sum / 10;
            cur->next = new ListNode(sum % 10);
            cur = cur->next;
            if(rl1) rl1 = rl1->next;
            if(rl2) rl2 = rl2->next;
        }
        if(carry) cur->next = new ListNode(1);
        return ReverseLists(dummy->next);
    }
};
  • 思路二:不对链表中的节点反转,使用栈进行辅助操作
/**
 * 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> st1, st2, res;
        while(l1)
        {
            st1.push(l1->val);
            l1= l1->next;
        } 
        while(l2)
        {
            st2.push(l2->val);
            l2 = l2->next;
        }

        int carry = 0;
        while(!st1.empty() || !st2.empty())
        {
            int n1 = st1.empty() ? 0 : st1.top();
            int n2 = st2.empty() ? 0 : st2.top();
            int sum = n1 + n2 + carry;
            carry = sum / 10;
            res.push(sum % 10);
            if(!st1.empty()) st1.pop();
            if(!st2.empty()) st2.pop();
        }
        if(carry) res.push(1);

        ListNode *dummy = new ListNode(-1);
        auto cur = dummy;
        while(!res.empty())
        {
            cur->next = new ListNode(res.top());
            res.pop();
            cur = cur->next;
        }
        return dummy->next;
    }
};

//写法二
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        stack<int> st1, st2;
        while(l1)
        {
            st1.push(l1->val);
            l1= l1->next;
        } 
        while(l2)
        {
            st2.push(l2->val);
            l2 = l2->next;
        }

        int carry = 0;
        ListNode *dummy = new ListNode(-1);
        while(!st1.empty() || !st2.empty() || carry != 0)
        {
            int n1 = st1.empty() ? 0 : st1.top();
            int n2 = st2.empty() ? 0 : st2.top();
            int sum = n1 + n2 + carry;
            carry = sum / 10;
            ListNode *cur = new ListNode(sum % 10);
            //让链表中的下一个指向上一个,并用虚拟节点记录
            cur->next = dummy->next;
            dummy->next = cur;
            if(!st1.empty()) st1.pop();
            if(!st2.empty()) st2.pop();    
        }
        return dummy->next;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值