当前位置:网站首页>删除链表的倒数第N个节点---2022/02/22
删除链表的倒数第N个节点---2022/02/22
2022-06-11 17:18:00 【城猪猪】
题目描述:
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
JAVA实现如下:
package czz;
/* * 日期:2022.02.22 * 作者:城主 * 题目:给定一个链表删除倒数第n个节点并返回头节点 * 思考:由于java和C还是有所区别,少了指针的概念,那么在 * java中使用何种结构来表示C语言中的指针概念? * * 清楚了节点之间的连接关系,由于是倒数第n个节点,所以, * 首先定位到第len-n个位置的节点,直接做删除处理。 * * 收获: * 1、java中如何创建链表,在节点类中添加add的方法。 */
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val; }
ListNode(int val, ListNode next) {
this.val = val; this.next = next; }
//添加结点
public void add(int newdata){
ListNode newNode = new ListNode(newdata); //创建一个结点
if(this.next == null){
this.next = newNode;
}
else{
this.next.add(newdata);
}
}
}
public class DeleteNode_0222 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] str= {
1,2,3,4,5};
int n=2;
ListNode head=new ListNode(1,null);
for(int i=1;i<str.length;i++) {
head.add(str[i]);
}
}
public static ListNode removeNthFromEnd(ListNode head, int n) {
/* * 定义快慢指针,快指针先走N步,如果走到末端说明去除头一个节点 * 此时直接做返回。否则,快指针继续走到末端,然后慢指针走到待删 * 除节点的前一个节点。而后直接做删除处理。 */
ListNode fast=head;
ListNode slow=head;
while(n--!=0)
{
fast=fast.next;
}
if(fast==null)
{
return head.next;
}
fast=fast.next;
while(fast!=null)
{
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return head;
}
}
性能评估如下:
以内存换取时间。
边栏推荐
- How to simplify a lot of if... Elif... Else code?
- Authoring share | understanding saml2 protocol
- Xie Yang, CEO of authing, was selected into Forbes' 30 under 30 Asia list in 2021
- 6-1 从文件读取字符串(*)
- A simple understanding of closures
- Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“
- 使用exe4j 将.jar文件打包为.exe文件
- Authing CEO 谢扬入选福布斯 2021 年 30 Under 30 亚洲榜单
- Go path: goroot & gopath
- 论文阅读 dyngraph2vec: Capturing Network Dynamics using Dynamic Graph Representation Learning
猜你喜欢

Chapter II relational database

论文阅读 dyngraph2vec: Capturing Network Dynamics using Dynamic Graph Representation Learning

DFS and BFS notes (I) breadth first search based on C language

满k叉树编号为 i 的节点的孩子编号公式推导

Recyclerview cache reuse analysis, source code interpretation

vscode配置eslint自动格式化报错“Auto Fix is enabled by default. Use the single string form“

Read and understand the development plan for software and information technology service industry during the "14th five year plan"

拜登下令强制推行零信任架构

Xie Yang, CEO of authing, was selected into Forbes' 30 under 30 Asia list in 2021

DFS和BFS笔记(一)基于C语言的广度优先搜索
随机推荐
MATLAB中histogram函数的使用
04_ Feature engineering feature selection
ffmpeg硬编解码 Inter QSV
leetcode--数组
Format eslint automatique lorsque vscode enregistre le Code
LeetCode-384. 打乱数组
mysql 大表的拆分方式
What subclasses inherit, polymorphism, and upward transformation
From a "trendsetter" to a "wind chaser", can master Kang still lead the market?
Activity | authing's first channel cooperation activity came to a successful conclusion
Authoring share | understanding saml2 protocol
Science popularization genius on the left, madman on the right
Computing philosophy behind authoring
为什么udp流设置1316字节
6-7 文件读写操作
Qlineedit set input mask
sql server中关于FORCESCAN的使用以及注意项
Hands on deep learning - multiple input and output channels in the convolution layer
Axi protocol Basics
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice