当前位置:网站首页>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;
}
边栏推荐
- VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
- 力扣练习——59 从二叉搜索树到更大和树
- flask-restplus接口地址404问题
- [Go WebSocket] 多房间的聊天室(一)思考篇
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- [E-commerce operation] Do you really understand social media marketing (SMM)?
- LeetCode 86. 分隔链表
- 网络套接字(UDP和TCP编程)
- StoneDB 文档捉虫活动第一季
- [Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
猜你喜欢
随机推荐
Licking Exercise - 58 Verifying Binary Search Trees
即时零售业态下如何实现自动做账?
Not just running away, but saving the guy who mishandled rm -rf /*
APP automation testing practice based on UiAutomator2+PageObject mode
有哪些好用的性能测试工具推荐?性能测试报告收费标准
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
单目操作符(含原码反码补码转换)
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
不止跑路,拯救误操作rm -rf /*的小伙儿
Flutter气泡框实现
Article take you understand interrupt the key driver of polling mechanism
StoneDB Document Bug Hunting Season 1
LeetCode 83. 删除排序链表中的重复元素
从源码角度分析UUID的实现原理
个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
Analysis of the name matching process between the LCD driver and the device (Tiny4412)
Module 9 - Designing an e-commerce seckill system
LAXCUS分布式操作系统安全管理
电脑怎么设置屏幕息屏时间(日常使用分享)
一文读懂NFT数字藏品为何风靡全球?









