当前位置:网站首页>剑指 Offer II 025. 链表中的两数相加
剑指 Offer II 025. 链表中的两数相加
2022-06-25 12:19:00 【小白码上飞】
一句话概要
先反转链表,然后在从头开始加和进位。
题目
给定两个 非空链表 l1和 l2 来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
可以假设除了数字 0 之外,这两个数字都不会以零开头。

链接:https://leetcode.cn/problems/lMSNwu
思路
其实就是两个数字做加法。但是正常计算都是从右往左加和、进位,但是用链表表示一个数字,就只能从左往右去计算数字。所以先反转两个链表,然后一起移动加和,就可以了。
解法:先反转链表,再加和
代码
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1 = reverseList(l1);
l2 = reverseList(l2);
ListNode next = null;
int add = 0;
while (l1 != null || l2 != null) {
int sum = add;
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}
add = sum / 10;
ListNode current = new ListNode(sum % 10);
current.next = next;
next = current;
}
if (add != 0) {
ListNode current = new ListNode(add);
current.next = next;
return current;
}
return next;
}
public ListNode reverseList(ListNode head) {
ListNode current = head;
ListNode pre = null;
ListNode next;
while (current != null) {
next = current.next;
current.next = pre;
pre = current;
current = next;
}
return pre;
}
边栏推荐
- Draw the satellite sky map according to the azimuth and elevation of the satellite (QT Implementation)
- (5) Pyqt5 ---- another method of connecting signals and slots
- Command line garbled
- My first experience of go+ language -- a collection of notes on learning go+ design architecture
- [Visio]平行四边形在Word中模糊问题解决
- 浏览器的5种观察器
- 地理空间搜索:kd树的实现原理
- Summary of common MySQL database commands (from my own view)
- 高性能负载均衡架构如何实现?
- PPT绘论文图之导出分辨率
猜你喜欢

MySQL and excel tables importing database data (Excel for MySQL)

Wechat full-text search technology optimization

AI assisted paper drawing of PPT drawing
![[visio] solving the fuzzy problem of parallelogram in word](/img/04/8a1de2983d648e67f823b5d973c003.png)
[visio] solving the fuzzy problem of parallelogram in word
模块五(微博评论)

Why are databases cloud native?

(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
![按权重随机选择[前缀和+二分+随机target]](/img/84/7f930f55f8006a4bf6e23ef05676ac.png)
按权重随机选择[前缀和+二分+随机target]

线上服务应急攻关方法论
![[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1](/img/36/167397ce61240036c865dd99463f1b.jpg)
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1
随机推荐
[visio] solving the fuzzy problem of parallelogram in word
Initialize the project using the express framework
STM32 在flash中存储float数据
How to implement a high-performance load balancing architecture?
Matlab simulation of m-sequence
list. replace, str.append
mysql导入导出数据到excel表日期出现问题
Baidu search stability analysis story
My first experience of go+ language -- a collection of notes on learning go+ design architecture
The drop-down box renders numbers instead of the corresponding text. How to deal with it
Circular exercises of JS
三入职场!你可以从我身上学到这些(附毕业Vlog)
Three jobs! You can learn this from me (attached with graduation vlog)
Another night when visdom crashed
Jenkins Pipeline使用
Laravel task scheduling
(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code
QT TCP UDP network communication < theory >
架构师必备的七种能力
Micro engine remote attachment 7 Niu cloud upload