当前位置:网站首页>【刷题记录】2. 两数相加
【刷题记录】2. 两数相加
2022-07-06 21:33:00 【InfoQ】
一、题目描述

- 每个链表中的节点数在范围 [1, 100] 内
0 <= Node.val <= 9
- 题目数据保证列表表示的数字不含前导零
二、思路分析:

- l1 就是[3,4] l2 就是[7,2] 链表中对应位置的相加,就是数字运算过程中,由低位向高位相加的一个过程。
- 如果相加有进位数时候,我们要记录进位数,并且在一次的运行中相加上这个进位数 就是上图中的1(tens)
- 然后把每次的结果存入到一个新的链表中 比如上图中结果为70 就是 [0,7]
三、代码实现
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// 输出结果的链表
ListNode resultNode = new ListNode(0);
ListNode head = resultNode;
// 记录进位数
int carryNum = 0;
while (l1 != null || l2 != null){
// 判断两个链表中对应位置是否存在值 (可能连个链表位数不一致)
int num1 = l1 != null ? l1.val : 0;
int num2 = l2 != null ? l2.val : 0;
// 对应位置上的数字相加 以及加上进位数
int tmpnum = num1 + num2 + carryNum;
// 记录对应数字到链表
head.next = new ListNode(tmpnum % 10 );
//获取进位数
carryNum = tmpnum / 10;
head = head.next;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
}
// 如果最高位还有进位 补位。
if (carryNum > 0) head.next = new ListNode(carryNum);
return resultNode.next;
}
}

总结
边栏推荐
- Redis源码学习(31),字典学习,dict.c(一)
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- Using thread class and runnable interface to realize the difference between multithreading
- QT opens a file and uses QFileDialog to obtain the file name, content, etc
- HW-小记(二)
- leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
- Clock in during winter vacation
- Force buckle ----- path sum III
- golang 根据生日计算星座和属相
- 机器学习笔记 - 使用机器学习进行鸟类物种分类
猜你喜欢

再AD 的 界面顶部(菜单栏)创建常用的快捷图标

QT 项目 表格新建列名称设置 需求练习(找数组消失的数字、最大值)

The most complete learning rate adjustment strategy in history LR_ scheduler

Kalman filter-1

概率论公式

Arduino droplet detection
![[security attack and Defense] how much do you know about serialization and deserialization?](/img/1c/e5ae74e65bacf688d7f61cc1b71d3e.png)
[security attack and Defense] how much do you know about serialization and deserialization?

QT 打开文件 使用 QFileDialog 获取文件名称、内容等

【OA】Excel 文档生成器: Openpyxl 模块

VHDL implementation of arbitrary size matrix addition operation
随机推荐
Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
自适应非欧表征广告检索系统AMCAD
On file uploading of network security
ABAP 动态内表分组循环
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
10 ways of interface data security assurance
QT 打开文件 使用 QFileDialog 获取文件名称、内容等
codeforces每日5题(均1700)-第七天
HW-小记(二)
2022电工杯A题高比例风电电力系统储能运行及配置分析思路
map和set的实现
海思万能平台搭建:颜色空间转换YUV2RGB
API data interface of A-share index component data
什么是 BA ?BA怎么样?BA和BI是什么关系?
Docker部署Mysql8的实现步骤
VHDL implementation of arbitrary size matrix multiplication
Hisilicon 3559 universal platform construction: RTSP real-time playback support
Confirm the future development route! Digital economy, digital transformation, data This meeting is very important
How to detect whether the MySQL code runs deadlock +binlog view
Baidu map JS development, open a blank, bmapgl is not defined, err_ FILE_ NOT_ FOUND