当前位置:网站首页>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);
}
}边栏推荐
- Final assignment of R language data analysis in a university
- 记录本项目中用到的系统调用与C库函数-2
- 5G China unicom AP:B SMS ASCII 转码要求
- ArcEngine(八) 选择要素并高亮显示
- JS本地存储 sessionStorage和localStorage
- 5G China unicom repeater network management protocol real-time requirements
- 微服务+微信小程序实现社区服务
- 剑指 Offer 43. 1~n 整数中 1 出现的次数(递归、数学)
- The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)
- CPU-MIPS32指令架构(无内锁流水线微处理器)
猜你喜欢

gin's middleware and routing grouping

How to solve the 0x80070005 error when the computer is reinstalled and the system is restored

Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目

GIN file upload and return

技嘉显卡 RGBFusion 不能调光解决方法

npm install失败

现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?

Explanation of RTSP protocol

剑指offer,剪绳子2

5G 联通网管设计思路
随机推荐
The FPGA - work summary recently
Microsoft 10/11 命令行打开系统设置页(WUAP,!WIN32)
驻波比计算方法
Standing wave ratio calculation method
ftplib+ tqdm upload and download progress bar
问题系列-如何修改或更新localhost里的值
Q_06_05 文件结构
Clock frequency and baud rate count for serial communication in FPGA
第六届“强网杯”全国网络安全挑战赛
5G Unicom Network Management Design Ideas
缓存和数据库一致性问题
The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)
Rmarkdown Tutorial
剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)
面试攻略系列(三)-- 高级开发工程师面试问些啥?
陈强教授《机器学习及R应用》课程 第十三章作业
GIN Bind mode to get parameters and form validation
CPU-MIPS32 instruction architecture (unlocked pipeline microprocessor)
Sandbox中的进程/线程相关-1
陈强教授《机器学习及R应用》课程 第十六章作业