当前位置:网站首页>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;
}
边栏推荐
- 【小程序 | 启航篇】一文打通任督二脉
- 再有人问你分布式事务,把这篇扔给他
- SMIC CIM localization project suspended?Rising software: not shut down, changed to remote development!
- 可视化服务编排在金融APP中的实践
- 接口定义与实现
- Analysis of the implementation principle of UUID from the perspective of source code
- 十年架构五年生活-09 五年之约如期而至
- [E-commerce operation] Do you really understand social media marketing (SMM)?
- gpu-admission 源码分析
- 力扣练习——56 寻找右区间
猜你喜欢
使用哈工大LTP测试分词并且增加自定义字典
推荐6个自媒体领域,轻松易上手
接口定义与实现
学长告诉我,大厂MySQL都是通过SSH连接的
3款不同类型的自媒体免费工具,有效提高创作、运营效率
If someone asks you about distributed transactions again, throw this to him
WeChat applet, global variables change in one place and the state in other places also changes.
[Go WebSocket] 多房间的聊天室(一)思考篇
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
即时零售业态下如何实现自动做账?
随机推荐
单目操作符(含原码反码补码转换)
LeetCode 146. LRU 缓存
Nocalhost - 让云原生时代的开发更高效
基于UiAutomator2+PageObject模式开展APP自动化测试实战
制品库是什么?
Licking Exercise - 58 Verifying Binary Search Trees
石墨文档打开文档时快速定位到上次写的位置
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
LeetCode 237. 删除链表中的节点
2016,还是到了最后
Clicking Exercise - 64 Longest Harmonic Subsequences
网络套接字(UDP和TCP编程)
皕杰报表在传参乱码
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
配置druid数据源「建议收藏」
Buckle Exercise - 61 Sort by frequency of characters
HDU 4135: Co-prime (the principle of inclusion and exclusion)
嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。