当前位置:网站首页>LeetCode 445. 两数相加 II
LeetCode 445. 两数相加 II
2022-08-10 11:09:00 【水菜笔】
原题网址:https://leetcode.cn/problems/add-two-numbers-ii/submissions/
数字用链表表示,个位在链表尾;对这两个数做加法,返回新的链表
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode list1 = reverseList(l1);
ListNode list2 = reverseList(l2);
int mod=0;
ListNode head = null;
while(list1 != null || list2 != null) {
int num1 = list1 == null ? 0: list1.val;
int num2 = list2 == null ? 0: list2.val;
int sum = num1+num2+mod;
mod = sum/10;
ListNode node = new ListNode(sum%10);
node.next = head;
head = node;
list1 = list1 == null ? null : list1.next;
list2 = list2 == null ? null : list2.next;
}
if(mod !=0) {
ListNode node = new ListNode(1);
node.next = head;
head = node;
}
return head;
}
private ListNode reverseList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode cur = head;
ListNode first = null;
while(cur != null) {
ListNode next = cur.next;
cur.next = first;
first = cur;
cur = next;
}
return first;
}
边栏推荐
- Article take you understand interrupt the key driver of polling mechanism
- Licking Exercise - 60 Maximum key-value sum of binary search subtrees
- 从产品维度来看 我们为什么不能完全信任Layer2?
- AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
- L2 applications from a product perspective: why is it a playground?
- flask-restplus接口地址404问题
- 软件架构简介
- mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
- blocking non-blocking poll mechanism asynchronous
- 机器学习之暴力调参案例
猜你喜欢

AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)

OPNsense安装配置Zenarmor

StoneDB 文档捉虫活动第一季

使用哈工大LTP测试分词并且增加自定义字典

从源码角度分析UUID的实现原理

Get started quickly and conquer three different distributed architecture calling schemes

rider内Mono脚本找不到引用资源

蔚来-软件开发工程师一面记录

A case of violent parameter tuning in machine learning

Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
随机推荐
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
常量及数据类型你还记得多少?
Nocalhost - 让云原生时代的开发更高效
蔚来-软件开发工程师一面记录
HDU 6040 Hints of sd0061 (技巧)
LAXCUS分布式操作系统安全管理
从源码角度分析UUID的实现原理
再有人问你分布式事务,把这篇扔给他
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
力扣练习——60 二叉搜索子树的最大键值和
Unsafe的一些使用技巧
2022年裁员潮,失业程序员何去何从?
Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
OPNsense安装配置Zenarmor
怎么加入自媒体,了解这5种变现模式,让账号快速变现
leetcode 823. Binary Trees With Factors(因子二叉树)
The brave rice rice, does not fear the brush list of 】 list has a ring
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)