当前位置:网站首页>NC40 链表相加(二)
NC40 链表相加(二)
2022-08-09 13:02:00 【syc596】
NC40 链表相加(二)
链表相加(二)_牛客题霸_牛客网 (nowcoder.com)
2. 两数相加
import java.util.*;
public class Solution {
public ListNode reverse(ListNode head){
ListNode prev=null;
ListNode cur=head;
while(cur!=null){
ListNode next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
public ListNode addInList (ListNode head1, ListNode head2) {
head1=reverse(head1);
head2=reverse(head2);
ListNode vhead=new ListNode(-1);
ListNode cur=vhead;
int carry=0;
while(head1!=null||head2!=null){
int x=(head1==null)?0:head1.val;
int y=(head2==null)?0:head2.val;
int sum=x+y+carry;
carry=sum/10;
sum=sum%10;
cur.next=new ListNode(sum);
cur=cur.next;
if(head1!=null){
head1=head1.next;
}
if(head2!=null){
head2=head2.next;
}
}
if(carry==1){
cur.next=new ListNode(carry);
}
return reverse(vhead.next);
}
}
边栏推荐
- ArcEngine(八) 选择要素并高亮显示
- How to reduce the size of desktop icons after the computer is reinstalled
- 行程和用户[阅读理解法]
- GET POST PUT DELETE request in GIN
- Microsoft 10/11 命令行打开系统设置页(WUAP,!WIN32)
- Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
- 5G China unicom 一般性异常处理
- JS动画函数封装
- FFmpeg multimedia file processing (FFMPEG logging system)
- R 语言 2010.1至2021.12艾滋病每月发病人数 时间序列分析
猜你喜欢
RTSP协议讲解
【瑞吉外卖】day05:增、删、改、查分类以及公共字段自动填充
Unicom network management protocol block diagram
Come and throw eggs.
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
阿里大淘系模型治理阶段性分享
绘制混合密度函数图以及添加分位数线
FPGA-近日工作总结
GIN Bind mode to get parameters and form validation
随机推荐
FPGA - Summary of bugs in ISE (in update)
NFS pays special attention to the problem of permissions
陈强教授《机器学习及R应用》课程 第十四章作业
19、学习MySQL 索引
5G China unicom 一般性异常处理
The FPGA - work summary recently
关于做2D游戏时,Canvas边界显示在Game窗口的问题
IDEA Gradle 常遇问题(二)(持续更新)
GIN Bind mode to get parameters and form validation
5G China unicom AP:B SMS ASCII 转码要求
电脑重装系统还原0x80070005错误如何解决
render解析
Data Mining-06
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
群组行动控制--自动队列化实现策略
【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
RTSP协议的实现
面试攻略系列(二)-- 秒杀系统
FFmpeg多媒体文件处理(FFMPEG日志系统)
面试攻略系列(四)-- 你不知道的大厂面试