当前位置:网站首页>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;
}
边栏推荐
- 力扣练习——61 根据字符出现频率排序
- HDU 4135: Co-prime (the principle of inclusion and exclusion)
- 态路小课堂丨如何为CXP光模块选择光纤跳线?
- 有哪些好用的性能测试工具推荐?性能测试报告收费标准
- Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
- Cannot find symbol log because lombok is not found
- 机器学习之暴力调参案例
- 一文读懂NFT数字藏品为何风靡全球?
- mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
猜你喜欢

Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇

做自媒体月入几万?博主们都在用的几个自媒体工具

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

mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull

石墨文档打开文档时快速定位到上次写的位置

微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。

孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感

基于UiAutomator2+PageObject模式开展APP自动化测试实战

自媒体爆款标题怎么写?手把手教你写热门标题

微信小程序提交审核历史版本记录从哪里查看
随机推荐
实现内网穿透的最佳解决方案(无实名认证,完全免费)
基于UiAutomator2+PageObject模式开展APP自动化测试实战
LeetCode 19. Delete the Nth last node of the linked list
SQL优化最强总结 (建议收藏~)
Buckle Exercise - 61 Sort by frequency of characters
力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
因为找不到lombok而找不到符号log
Network sockets (UDP and TCP programming)
gpu-admission 源码分析
苹果逆势扩大iPhone 14系列备货,总量或达9500万部
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
LeetCode 61. 旋转链表
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
WeChat applet, global variables change in one place and the state in other places also changes.
三星计划2023年开始在越南生产半导体零部件
Configure druid data source "recommended collection"
Nocalhost - 让云原生时代的开发更高效
[E-commerce operation] Do you really understand social media marketing (SMM)?
LeetCode 362. Design Hit Counter
力扣练习——60 二叉搜索子树的最大键值和