算法题_两个链表相加

一、题目分析

在这里插入图片描述

1.1 listLength 获得链表长度

在这里插入图片描述

listLength方法入参为链表的头节点,返回此链表的长度。

1.2 addTwoNumbers 主程序

在这里插入图片描述

入参为两个链表的头节点,

  1. 通过listLength()方法获得链表的长度,用来区长链表l和短链表s,并设置临时变量carry,先从短链表开始相加,curNum表示相加之后的和,通过取模的余数即为该位置上的数字,通过除以10来获得进位的数值。
  2. 短链表结束后,此时计算剩下长链表的值:长链表的值加上进位值。
  3. last变量记录链表最后位置的元素,当长链表结束后,判断进位是否为1,如果进位为一,需要新增一位元素。

二、代码实现

package com.lsh.day05;

/**
 * @author :LiuShihao
 * @date :Created in 2022/2/6 4:32 下午
 * @desc :题目2:两个链表相加
 */
public class Code06 {
    public static class ListNode{
        public int value;
        public ListNode next;
        public ListNode(int v){
            value = v;
            next = null;
        }
    }

    /**
     * 两个链表相加
     * 分为三种情况:
     * 1.长短链表有元素。
     * 2.长链表有元素,端链表无元素。
     * 3.长短链表无元素,但是进位为1。
     * @param head1
     * @param head2
     * @return
     */
    public static ListNode addTwoNumbers(ListNode head1,ListNode head2){
        int len1 = listLength(head1);
        int len2 = listLength(head2);

        ListNode l = len1 >= len2 ? head1:head2;
        ListNode s = l == head1 ? head2 : head1;

        ListNode curL = l;
        ListNode curS = s;
        ListNode last = curL;

        //进位
        int carry = 0;
        int curNum= 0;

        while (curS != null){
            curNum = curL.value + curS.value + carry;
            //余数
            curL.value = (curNum % 10);
            //进位
            carry = curNum / 10;
            last = curL;
            curS = curS.next;
            curL = curL.next;
        }

        while (curL != null){
            curNum = curL.value + curS.value + carry;
            //余数
            curL.value = (curNum % 10);
            //进位
            carry = curNum / 10;
            last = curL;
            curL = curL.next;
        }
        if (carry != 0){
            last.next = new ListNode(1);
        }
        return l;
    }



    /**
     * 获得链表长度
     * @param head
     * @return
     */
    public static int listLength(ListNode head){
        int len = 0;
        while (head != null){
            len++;
            head = head.next;
        }
        return len;
    }


}

三、验证

 /**
     *  从左到右是数字的低位到高位
     *   999 + 001 = 1000
     *   9->9->9 + 1->0->0 = 0->0->0->1
     * @param args
     */
    public static void main(String[] args) {
        ListNode head1 = new ListNode(9);
        head1.next = new ListNode(9);
        head1.next.next = new ListNode(9);
        ListNode head2 = new ListNode(1);
        head2.next = new ListNode(0);
        head2.next.next = new ListNode(0);
        ListNode newHead = addTwoNumbers(head1, head2);

        while (newHead!= null){
            System.out.print(newHead.value + " ");
            newHead = newHead.next;
        }
        System.out.println();
    }

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liu_Shihao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值