当前位置:网站首页>剑指 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;
}
边栏推荐
- 线上服务应急攻关方法论
- 515. Find Largest Value in Each Tree Row
- Differences between JS and JQ operation objects
- 2021-09-30
- 1251- Client does not support authentication protocol MySql错误解决方案
- The push(), pop(), unshift(), shift() method in the JS array
- Render values to corresponding text
- Swagger document generated by node project API in vscode
- CUDA error: unspecified launch failure
- Slice and slice methods of arrays in JS
猜你喜欢

(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code

Total number of MySQL statistics, used and unused

Guess Tongyuan B

GNSS receiver technology and application review

Swagger document generated by node project API in vscode

Go from 0 to 1. Obtain the installation package, get, post request, params, body and other parameters

百度搜索稳定性问题分析的故事

利用cmd(命令提示符)安装mysql&&配置环境

MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r

Laravel excel export
随机推荐
High performance + million level Excel data import and export
C program linking SQLSERVER database: instance failed
The editor is used every day. What is the working principle of language service protocol?
2021-10-21
515. Find Largest Value in Each Tree Row
Update PIP & Download jupyter Lab
laravel 9
Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
JS array de duplication
3+1保障:高可用系统稳定性是如何炼成的?
Micro engine remote attachment 7 Niu cloud upload
(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
Summary of common MySQL database commands (from my own view)
20220620 interview reply
Wechat forbids sharing
Match regular with fixed format beginning and fixed end
Flutter automatically disappears after receiving push
515. Find Largest Value in Each Tree Row
The amount is verified, and two zeros are spliced by integers during echo
阿里稳定性之故障应急处理流程