type
status
date
slug
summary
tags
category
icon
password
😀
两个逆序存储的单向链表相加返回求和 返回也是逆序的

2. Add Two Numbers2. 将两个数字相加

Solved  已解决
Medium  中等的
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
给定两个非空链表,分别表示两个非负整数。链表中的数字按逆序存储,每个节点包含一个数字。求这两个整数的和,并将和以链表的形式返回。
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
您可以假设这两个数字除了数字 0 本身之外,不包含任何前导零
notion image
notion image
notion image
思路很清晰啊
遍历两个列表对吧
很简单 找到头 然后呢? 加啊 但是里面有个问题就是
你想下 如果是1+1=2 那没事 但是呢?1+19=10 是不是要进位啊 要进位那下次加的时候是不是就得加上进位的数字那你是不是就得想下了
再想下
2》3》4》5》6》7 是不是就是765432这个数字
9》3》1 是不是就是139这个数字
那么说相加就是9+2 也就是列表1节点1+列表2节点1 结果是11 但是我们的链表不可以存储两位
11怎么样呢 其中就有一个概念就是向下取整对吧 什么会呢?当然是我们亲爱的除法
但是我们如何去判断下次要不要+1呢 简单对吧
我们定义一个变量去每次去存储 然后外部的变量计算进位就这吧开始gogogo
notion image
paper Writing notion两数之和
Loading...