当前位置:网站首页>LeetCode 445. Adding Two Numbers II
LeetCode 445. Adding Two Numbers II
2022-08-10 11:58:00 【Mizuna pen】
原题网址:https://leetcode.cn/problems/add-two-numbers-ii/submissions/
Numbers are represented by linked lists,units at the end of the linked list;Add these two numbers,返回新的链表
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;
}
边栏推荐
- 嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
- Centos7环境使用Mysql离线安装包安装Mysql5.7
- LeetCode 25. K 个一组翻转链表
- 零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
- SMIC CIM localization project suspended?Rising software: not shut down, changed to remote development!
- AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
- 面试官:你们是如何保证接口的幂等性?
- 【机器学习】浅谈正规方程法&梯度下降
- 力扣练习——59 从二叉搜索树到更大和树
- Stroke Practice - 62 Valid Sudokus
猜你喜欢

A little self-deprecating deconstruction about farmers "code"

Since the media hot style title how to write?Taught you how to write the title

快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?

WeChat applet, global variables change in one place and the state in other places also changes.

接口定义与实现

Nocalhost - Making development more efficient in the cloud-native era

机器学习之暴力调参案例

学长告诉我,大厂MySQL都是通过SSH连接的

Nocalhost - 让云原生时代的开发更高效

电脑怎么设置屏幕息屏时间(日常使用分享)
随机推荐
Where can I view the version record of WeChat applet submission review history?
制品库是什么?
ssm框架搭建过程[通俗易懂]
codevs 2370 Small room tree (LCA)
Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
3款不同类型的自媒体免费工具,有效提高创作、运营效率
使用JMeter进行MySQL的压力测试
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
不止跑路,拯救误操作rm -rf /*的小伙儿
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
Flutter气泡框实现
LCD驱动端与设备端名称匹配过程分析(Tiny4412)
[Go WebSocket] 多房间的聊天室(一)思考篇
一文读懂NFT数字藏品为何风靡全球?
配置druid数据源「建议收藏」
gpu-admission 源码分析
基于UiAutomator2+PageObject模式开展APP自动化测试实战
使用.NET简单实现一个Redis的高性能克隆版(六)