当前位置:网站首页>NC53 删除链表的倒数第n个节点
NC53 删除链表的倒数第n个节点
2022-08-09 13:02:00 【syc596】
NC53 删除链表的倒数第n个节点
删除链表的倒数第n个节点_牛客题霸_牛客网 (nowcoder.com)
//11
//快慢指针
import java.util.*;
public class Solution {
public ListNode removeNthFromEnd (ListNode head, int n) {
ListNode slow=head;
ListNode fast=head;
for(int i=0;i<n;i++){
fast=fast.next;
}
//要删除的节点是头节点
if(fast==null){
return head.next;
}
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next;
}
slow.next=slow.next.next;
return head;
}
}
// //长度统计
// import java.util.*;
// public class Solution {
// public ListNode removeNthFromEnd (ListNode head, int n) {
// ListNode cur=head;
// int len=0;
// while(cur!=null){
// len++;
// cur=cur.next;
// }
// if(len-n==0){
// return head.next;
// }
// cur=head;
// for(int i=0;i<len-n-1;i++){
// cur=cur.next;
// }
// cur.next=cur.next.next;
// return head;
// }
// }
边栏推荐
猜你喜欢
Oracle Recovery Tools修复空闲坏块
Unity3d_API_GPS_LocationService
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)
WSA工具箱安装应用商店提示无法工作怎么解决?
jenkins api create custom pipeline
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
npm install失败
RTSP协议讲解
5G China unicom AP:B SMS ASCII 转码要求
[MRCTF2020]套娃-1
随机推荐
R语言kaggle 游戏数据探索与可视化
43. The sword refers to Offer 1 ~ 1 the number of occurrences of n integers (recursive, mathematics)
技嘉显卡 RGBFusion 不能调光解决方法
FFmpeg多媒体文件处理(FFMPEG日志系统)
npm install失败
【FPGA教程案例48】图像案例8——基于FPGA的RGB图像转化为HSV图像的实现,通过MATLAB进行辅助验证
陈强教授《机器学习及R应用》课程 第十四章作业
CPU-MIPS32指令架构(无内锁流水线微处理器)
七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
联通网管协议框图
Map mixed density function and quantile added line
CPU-MIPS32 instruction architecture (unlocked pipeline microprocessor)
The FPGA - work summary recently
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
NFS 特别注意权限的问题
行程和用户[阅读理解法]
FPGA中串口通信的时钟频率和波特率计数
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)
时间序列分析课程实验报告
剑指 Offer 56 - II. 数组中数字出现的次数 II(位运算)