两个链表生成相加链表——NC.40

本文介绍了一种解决链表表示的大数相加问题的方法。通过先反转链表,然后逐节点相加并处理进位,最后再反转回正确顺序得到结果链表。文章提供了完整的Java实现代码。

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

题目描述

假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。 给定两个这种链表,请生成代表两个整数相加值的结果链表。
例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。

起初想把链表转为整数,然后相加后再转为链表,然后我果然错了,这种题考的就是大数相加,这样解决还是大数相加,并没有真正解决问题。

解题思路:将两个链表反转,然后逐个节点的相加,注意进位标志。然后检查没算完的链表,注意进位标志。最后还是不要忘记进位标志。在构建结果时可以从后插入构建链表,这样构建后就是结果。

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        if (head1 == null) return head2;
        if (head2 == null) return head1;
        ListNode node1 = reverse(head1);
        ListNode node2 = reverse(head2);
        ListNode res = new ListNode(-1);
        int index = 0;    // 进位数
        while (node1 != null && node2 != null) {
            // 注意要加上进位标记
            int sum = node1.val + node2.val + index;
            if (sum >= 10) {
                index = sum / 10;
            } else {    // 没有进位要清除进位标记
                index = 0;
            }
            // 从后插入节点
            insertNode(res, sum % 10);
            
            node1 = node1.next;
            node2 = node2.next;
        }
        // 当两个链表长度不一致时
        // 这里的index要保持更新,不然会出错
        index = whenNodeNotNull(res, node1, index);
        index = whenNodeNotNull(res, node2, index);
        // 如果最后还有进位
        if (index != 0) {
            insertNode(res, index);
        }
        return res.next;
    }
    /**
    * 反转链表
    * @param head 被反转的链表
    */
    public ListNode reverse(ListNode head) {
        ListNode res = null;
        while (head != null) {
            ListNode p = head.next;
            head.next = res;
            res = head;
            head = p;
        }
        return res;
    }
    
    /**
    * 计算剩余链表
    * @param res 存储结果链表
    * @param target 被计算的链表
    * @param index 进位标志
    */
    public int whenNodeNotNull(ListNode res, ListNode target, int index) {
        while (target != null) {
            int sum = target.val + index;
            if (sum >= 10) {
                index = sum / 10;
            } else {
                index = 0;
            }
            
            insertNode(res, sum % 10);
            
            target = target.next;
        }
        return index;
    }
    
    /**
    * 从后向前插入节点
    * @param res 存储结果链表
    * @param val 节点数值
    */
    public void insertNode(ListNode res, int val) {
        ListNode cur = new ListNode(val);
        cur.next = res.next;
        res.next = cur;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值