当前位置:网站首页>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;
}
边栏推荐
- LeetCode 24. 两两交换链表中的节点
- 力扣练习——64 最长和谐子序列
- Nocalhost - Making development more efficient in the cloud-native era
- 配置swagger
- 配置druid数据源「建议收藏」
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- StoneDB Document Bug Hunting Season 1
- LeetCode 362. Design Hit Counter
- Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
- 嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
猜你喜欢
随机推荐
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
怎么加入自媒体,了解这5种变现模式,让账号快速变现
力扣练习——64 最长和谐子序列
Analysis of the name matching process between the LCD driver and the device (Tiny4412)
LeetCode 24. 两两交换链表中的节点
LeetCode 92. 反转链表 II
个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
Buckle Exercise - 61 Sort by frequency of characters
LeetCode 138. 复制带随机指针的链表
LeetCode 109. 有序链表转换二叉搜索树
Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
OSSCore 开源解决方案介绍
Where can I view the version record of WeChat applet submission review history?
A case of violent parameter tuning in machine learning
Configure druid data source "recommended collection"
接口定义与实现
Since the media hot style title how to write?Taught you how to write the title
面试官:你们是如何保证接口的幂等性?
Network Fundamentals (Section 1)
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?