当前位置:网站首页>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;
// }
// }
边栏推荐
- Process/Thread related in Sandbox - 1
- Clock frequency and baud rate count for serial communication in FPGA
- The FPGA - work summary recently
- npm install失败
- glibc 内存管理模型 释放 C库内存缓存
- ArcEngine(八) 选择要素并高亮显示
- gin's middleware and routing grouping
- 陈强教授《机器学习及R应用》课程 第十八章作业
- Explanation of RTSP protocol
- Microsoft 10/11 命令行打开系统设置页(WUAP,!WIN32)
猜你喜欢
随机推荐
ArcEngine(十)创建矢量图层
5G China unicom AP:B SMS ASCII 转码要求
2.微服务'黑话'集锦及Eureka注册中心相关概念
Yocto 可以下载的第三方库
电脑重装系统还原0x80070005错误如何解决
read stream 特别注意
联通网管协议框图
kustomize入门示例及基本语法使用说明
An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)
Rmarkdown教程
javscript基础易错点集合
自己做了个nodejs+epxress+mysql的小项目,怎么才能让别人通过互联网访问呢?
基于 R 语言的深度学习——简单回归案例
第六届”蓝帽杯“全国大学生网络安全技能大赛 半决赛
FPGA-在ISE中错误总结(更新中)
Come and throw eggs.
Draw a histogram with plot_hist_numeric()
LeetCode 37. Solve Sudoku
剑指offer,剪绳子2
Professor Chen Qiang the machine learning and R application course chapter 18 assignments