当前位置:网站首页>LeetCode链表问题——19.删除链表的倒数第N个节点(一题一文学会链表)
LeetCode链表问题——19.删除链表的倒数第N个节点(一题一文学会链表)
2022-07-26 19:58:00 【十八岁讨厌Java】
一、题目描述:
难度中等2139收藏分享切换为英文接收动态反馈
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

二、代码
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
// 因为删除可能涉及到头节点,所以设置dummy节点,统一操作
ListNode dummy = new ListNode(-1, head);
ListNode pre = dummy;
ListNode cur = head;
while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return dummy.next;
}三、了解链表
什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。链接的入口节点称为链表的头结点也就是head。
单链表:
双链表:
每一个节点有两个指针域,一个指向下一个节点,一个指向上一个节点。
双链表 既可以向前查询也可以向后查询。

循环链表:
循环链表,顾名思义,就是链表首尾相连。循环链表可以用来解决约瑟夫环问题。

链表的存储方式:
数组是在内存中是连续分布的,但是链表在内存中可不是连续分布的。
链表是通过指针域的指针链接在内存中各个节点。
所以链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。
删除节点:
只要将C节点的next指针 指向E节点就可以了。

添加节点:

与数组对比:

边栏推荐
- Ape tutoring's technological hard power: let AI start from reading children's homework
- Houdini 笔记2
- Bean注入和生命周期
- The lawyer team of the US Department of justice asked the judge to refuse to accept Huawei's lawsuit
- 第二章:遇到阻难!绕过WAF过滤!【SQL注入攻击】
- BUU刷题记-网鼎杯专栏2
- Houdini 求中点,点连成线
- Nmap installation and use
- 如何优雅地赞美他人?不妨尝试下这几种方式
- What are the advantages of digital factory
猜你喜欢

消息队列——引入的问题:重复消费&顺序消费&分布式事务

QT driving school subject examination system -- from implementation to release

Houdini 求中点,点连成线

Pandonia spirit voxedit creation competition

QT信号与槽连接(松耦合)

APP自动化测试框架搭建(八)--ATX Server2多设备集群环境搭建

Houdini notes 2

Software testing - development test content specification (project test template)

【【实验分享】CCIE—BGP路由黑洞实验】

Houdini 笔记2
随机推荐
【微信小程序】零基础学 | 小程序语法
twenty million two hundred and twenty thousand seven hundred and twenty-six
81. (cesium home) cesium modifies the gray background (default blue)
Shell综合应用案例,归档文件
Leetcode's question brushing -- List summary
营销与销售文件管理以及工作流程解决方案
Buu brush inscription - WANGDING cup column 2
Confid+etcd to realize automatic discovery of high availability
Summary of message queue knowledge points
BGP的路由黑洞和防环
从零开始搭建etcd分布式存储系统+Web管理界面
Bean injection and lifecycle
如何组装一个注册中心?
Small scenes bring great improvement! Baidu PaddlePaddle easydl helps AI upgrade of manufacturing assembly line
QT驾校科目考试系统——从实现到发布
【Delphi】FMX Form的BorderStyles不同平台说明
Do employees have to compensate the company for losses when they resign? The 34 year old captain resigned and was claimed 10.66 million yuan by the company
Leetcode刷题之——链表总结
How to assemble a registry?
Marketing and sales document management and workflow solutions