当前位置:网站首页>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);
}
}
边栏推荐
- Oracle Recovery Tools修复空闲坏块
- 【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
- FFMPEG多媒体文件处理(ffmpeg文件的删除与重命名)
- Record the system calls and C library functions used in this project-2
- 电脑重装系统后桌面图标如何调小尺寸
- glibc memory management model freeing C library memory cache
- 5G China unicom 一般性异常处理
- 陈强教授《机器学习及R应用》课程 第十三章作业
- FFmpeg multimedia file processing (ffmpeg prints audio and video Meta information)
- jenkins api create custom pipeline
猜你喜欢
面试攻略系列(三)-- 高级开发工程师面试问些啥?
农村区县域农业电商如何做?数字化转型如何进行?
剑指offer,剪绳子2
Oracle Recovery Tools修复空闲坏块
GIN file upload and return
RTSP协议讲解
第六届“强网杯”全国网络安全挑战赛
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)
随机推荐
ArcEngine(九)图形绘制
正则表达式-re模块
gin的中间件和路由分组
offset、client、scroll、window.pageYOffset比较
基于 R 语言的判别分析介绍与实践 LDA和QDA
ftplib+ tqdm upload and download progress bar
gin's middleware and routing grouping
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
5G Unicom Network Management Design Ideas
Dry+Bean+Dataset R语言数据分析,报告英文
ARM板卡增加路由功能
陈强教授《机器学习及R应用》课程 第十七章作业
周末看点回顾|我国IPv6网络全面建成;2022昇腾AI开发者创享日·南京站成功举办…
电脑重装系统后桌面图标如何调小尺寸
万物皆可柯里化的 Ramda.js
快来扔鸡蛋。
puzzle(016.5)逻辑电路
FPGA - Summary of bugs in ISE (in update)
昇腾AI开发者创享日南京站!一起CANN机器狗+AI机械臂实现硬核智慧救援!燃爆现场~
glibc memory management model freeing C library memory cache