LeetCode 两数相加

本文使用 Zhihu On VSCode 创作并发布

题目描述

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

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

示例:

v2-d9a4e072f401c16c9b22fc9590428af1_b.png
Image


链表定义:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

解题思路:

  • 由于数字逆序给出,所以从低位依次相加即可;
  • 需要解决进位、某数字是否已经加完等判断;
  • 应直接使用链表进行相加操作,可处理大数,若转化为整型会溢出;

源代码:

import java.util.Scanner;
import java.util.function.IntToDoubleFunction;

import javax.lang.model.util.ElementScanner6;


public class twoNumAdd {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入第一个数字:");
        long value1 = in.nextLong();
        System.out.println("输入第二个数字");
        long value2 = in.nextLong();
        ListNode l1 = solusion.longToList(value1);
        ListNode l2 = solusion.longToList(value2);
        ListNode l3 = solusion.addTwoNumbers(l1, l2);
        StringBuffer sb = solusion.listToSb(l3);
        System.out.println(sb);       
    }
}



class solusion {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //提交部分
        int temp = l1.val + l2.val;//最低位相加
        boolean add;//进位标识
        ListNode l3;//加和链表
        if (temp >= 10) {//若低位相加超过10,则将temp低位赋给l3,同时进位标识置为true
            l3 = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(1))));
            add=true;
        } 
        else {//若低位相加未超过10,则temp唯一一位赋给l3,进位标志置为false
            l3 = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(0))));
            add = false;
        }
        ListNode L = l3;//存储链表头
        l1=l1.next;l2=l2.next;
        if (l1==null&&l2==null&&add){//若此时l1,l2已结束且add为真,进位
            l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(0))));
            l3 = l3.next;
            add = false;
        }
        while (l1!= null || l2!= null||add) {
            if (l1 != null && l2 != null) {//l1,l2均为加完
                if (add) {//若有进位,需额外+1
                    temp = l1.val + l2.val + 1;
                } else {
                    temp = l1.val + l2.val;
                }
                if (temp >= 10) {//同理
                    l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(1))));
                    add = true;
                } else {
                    l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(0))));
                    add = false;
                }
                l1 = l1.next;
                l2 = l2.next;
            }
            else if (l1 == null && l2 != null) {//l1已加完
                if (add) {//若有进位,额外+1
                    temp=l2.val+1;
                    if (temp >= 10) {//若+1后有进位,同理
                        l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(1))));
                        if (l2.next == null) {
                            l2.next = new ListNode(0);
                        }
                        l2 = l2.next;
                        add = true;
                    } else {
                        l3.next = new ListNode(l2.val + 1);
                        l2 = l2.next;
                        add = false;
                    }
                }
                else {
                    l3.next = new ListNode(l2.val);
                    l2 = l2.next;  
                }

            }
            else if (l1 != null && l2 == null) {//l2已加完
                if (add) {//同理
                    temp = l1.val + 1;
                    if (temp >= 10) {
                        l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(1))));
                        if (l1.next == null) {
                            l1.next = new ListNode(0);
                        }
                        l1 = l1.next;
                        add = true;
                    } else {
                        l3.next = new ListNode(l1.val + 1);
                        l1 = l1.next;
                        add = false;
                    }
                } else {
                    l3.next = new ListNode(l1.val);
                    l1 = l1.next;
                }

            } else if (l1 == null && l2 == null && add) {//l1,l2均已加完,但是还有进位
                l3.next = new ListNode(1);
                add = false;
            }
            l3 = l3.next;
        }

        return L;

    }
    

    public static ListNode longToList(long value) {//整型转化为链表
        String str = String.valueOf(value);
        int len = str.length();
        ListNode L = new ListNode(Integer.parseInt(String.valueOf(str.charAt(len - 1))));
        ListNode LHead = L;
        len--;
        while (len > 0) {
            L.next = new ListNode(Integer.parseInt(String.valueOf(str.charAt(len - 1))));
            len--;
            L = L.next;
        }
        return LHead;
    }

    public static StringBuffer listToSb(ListNode l) {//链表转化为字符串
        StringBuffer sb = new StringBuffer();
        while (l.next != null) {
            sb.append(String.valueOf(l.val));
            l = l.next;
        }
        sb.append(String.valueOf(l.val));
        return sb;
    }
}

ListNode:

public class ListNode {
    int val;
    ListNode next;

    ListNode(int l) {
        val = l;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值