剑指 Offer II 025. 链表中的两数相加

题目

给定两个 非空链表 l1和 l2 来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

可以假设除了数字 0 之外,这两个数字都不会以零开头。

示例

在这里插入图片描述
输入:l1 = [7,2,4,3], l2 = [5,6,4]
输出:[7,8,0,7]

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[8,0,7]

输入:l1 = [0], l2 = [0]
输出:[0]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lMSNwu
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

方法1:反转链表
Java实现
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode h1 = reverse(l1);
        ListNode h2 = reverse(l2);
        ListNode dummy = new ListNode();
        ListNode pre = dummy;

        int add = 0;
        while (add != 0 || h1 != null || h2 != null) {
            int a = h1 != null ? h1.val : 0;
            int b = h2 != null ? h2.val : 0;
            int var = a + b + add;
            pre.next = new ListNode(var % 10);
            pre = pre.next;
            add = var / 10;
            if (h1 != null) h1 = h1.next;
            if (h2 != null) h2 = h2.next;
        }
        return reverse(dummy.next);
    }

    public ListNode reverse(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;

        while (cur != null) {
            ListNode tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
}

在这里插入图片描述

方法2:栈

本来打算用long型来做,这样就不用反转链表,不过没想到链表太长了直接long型溢出了。
看了题解发现像这种逆序的题要优先想到栈。
后面生成链表的时候,要用前插法。

Java实现
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while (l1 != null) {
            stack1.push(l1.val);
            l1 = l1.next;
        }

        while (l2 != null) {
            stack2.push(l2.val);
            l2 = l2.next;
        }
        ListNode pre = null;
        int add = 0;
        while (!stack1.isEmpty() || !stack2.isEmpty() || add != 0) {
            int a = stack1.isEmpty() ? 0 : stack1.pop();
            int b = stack2.isEmpty() ? 0 : stack2.pop();
            int var = a + b + add;
            ListNode tmp = new ListNode(var % 10);
            tmp.next = pre;
            pre = tmp;
            add = var / 10;
        }
        return pre;
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表两数相加是一个经典的问题,我们可以通过遍历链表并进行逐位相加来解决。具体的步骤如下: 1. 定义一个新的链表作为结果的头结点,并初始化为空。 2. 定义两个针分别向两个链表的头结点,以及一个进位变量carry,初始值为0。 3. 遍历两个链表,直到两个链表都遍历完。 4. 在每个节点上,将两个链表对应位置的值相加,并加上进位值carry。 5. 计算当前位置的值以及进位值,更新结果链表以及进位值。 6. 如果某个链表已经遍历完,但另一个链表还有剩余节点,则将剩余节点的值与进位值相加,并更新结果链表。 7. 如果最后一个节点的相加结果产生了进位,则在结果链表末尾添加一个值为1的新节点。 8. 返回结果链表的头结点。 下面是C++代码实现: ```cpp struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(0); ListNode* curr = dummy; int carry = 0; while (l1 || l2) { int x = (l1) ? l1->val : 0; int y = (l2) ? l2->val : 0; int sum = carry + x + y; carry = sum / 10; curr->next = new ListNode(sum % 10); if (l1) l1 = l1->next; if (l2) l2 = l2->next; curr = curr->next; } if (carry > 0) { curr->next = new ListNode(carry); } return dummy->next; } ``` 这个算法的时间复杂度是O(max(m, n)),其m和n分别是两个链表的长度。空间复杂度是O(max(m, n)),用于存储结果链表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值