2.两数相加

本文讲述了如何解决LeetCode上的问题,即给定两个逆序排列的非空链表,将它们相加并返回一个新的逆序链表表示和。作者首先定义了链表节点类,然后提供了原始代码和修复后的代码,重点在于处理链表末尾的进位问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.两数相加

Problem_0002_AddTwoNumbers: https://leetcode.cn/problems/add-two-numbers/description

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

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

public class Problem_0002_AddTwoNumbers {
	public ListNode addTwoNumbers(ListNode head1, ListNode head2) {
		 int l1 = lenthNode(head1);
		 int l2 = lenthNode(head2);
		 // 使用L记录长的那条链表,S记录断的那条链表
		 ListNode L = l1 > l2 ? head1 : head2;
		 ListNode S = L == head1 ? head2 : head1;
		 // 新建两个指针,此处题意是要求的结果链表同样是倒序,如果留着L和S拼成结果后直接返回,如果不是倒序,就不需要再新建两个指针
		 ListNode curL = L;
		 ListNode curS = S;
		 // last紧跟长的链表curL,为了拼接最后一次进位
		 ListNode last = curL;
		 // 记录进位 
		 int carry = 0;
		 int sum;
		 while(curS != null) {
			 sum = curS.val + curL.val + carry;
			 curL.val = sum % 10;
			 carry = sum / 10;
			 curL = curL.next;
			 curS = curS.next;
			 last = curL;
		 }
		 while(curL != null) {
			 sum = curL.val + carry;
			 curL.val = sum % 10;
			 carry = sum / 10;
			 curL = curL.next;
			 last = curL;
		 }
		 if(carry != 0) {
			 last = new ListNode(carry);
		 }
		 return L;
    }
	public static int lenthNode(ListNode head) {
		int lenth = 0;
		while(head != null) {
			lenth++;
			head = head.next;
		}
		return lenth;
	}
}
//上述代码存在错误:last = curL在curL移动到下个节点后赋值,导致最后为null,last = new ListNode(carry)后与L链表断开连接

修改last = curL;的位置

public class Problem_0002_AddTwoNumbers {
    public ListNode addTwoNumbers(ListNode head1, ListNode head2) {
        int l1 = lenthNode(head1);
        int l2 = lenthNode(head2);
        // 使用L记录长的那条链表,S记录断的那条链表
        ListNode L = l1 > l2 ? head1 : head2;
        ListNode S = L == head1 ? head2 : head1;
        // 新建两个指针,此处题意是要求的结果链表同样是倒序,如果留着L和S拼成结果后直接返回,如果不是倒序,就不需要再新建两个指针
        ListNode curL = L;
        ListNode curS = S;
        // last紧跟长的链表curL,为了拼接最后一次进位
        ListNode last = curL;
        // 记录进位 
        int carry = 0;
        int sum;
        while(curS != null) {
            sum = curS.val + curL.val + carry;
            curL.val = sum % 10;
            carry = sum / 10;
            last = curL;
            curL = curL.next;
            curS = curS.next;
        }
        while(curL != null) {
            sum = curL.val + carry;
            curL.val = sum % 10;
            carry = sum / 10;
            last = curL;
            curL = curL.next;
        }
        if(carry != 0) {
            last.next = new ListNode(carry);
        }
        return L;
    }
    public static int lenthNode(ListNode head) {
        int lenth = 0;
        while(head != null) {
            lenth++;
            head = head.next;
        }
        return lenth;
    }
}

image-20240103112621662

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值