当前位置:网站首页>LeetCode(剑指 Offer)- 18. 删除链表的节点
LeetCode(剑指 Offer)- 18. 删除链表的节点
2022-08-04 05:36:00 【放羊的牧码】
题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 亚马逊(中国)投资有限公司
AC 代码
- Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
// 解决方案(1)
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode tmp = head, pre = head;
if (head != null && head.val == val) {
return head.next;
}
while (tmp != null) {
if (tmp.val != val) {
pre = tmp;
tmp = tmp.next;
continue;
}
pre.next = tmp.next;
break;
}
return head;
}
}
// 解决方案(2)
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val) return head.next;
ListNode pre = head, cur = head.next;
while(cur != null && cur.val != val) {
pre = cur;
cur = cur.next;
}
if(cur != null) pre.next = cur.next;
return head;
}
}
- C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if(head->val == val) return head->next;
ListNode *pre = head, *cur = head->next;
while(cur != nullptr && cur->val != val) {
pre = cur;
cur = cur->next;
}
if(cur != nullptr) pre->next = cur->next;
return head;
}
};
边栏推荐
- 目标检测中的先验框(Anchor)
- 解决腾讯云DescribeInstances api查询20条记录以上的问题
- 【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
- 网络技巧:教你给路由器装上电池,断电照样可以上网!
- ffmpeg打开rtsp流应该设置的几个参数
- Faster - RCNN principle and repetition code
- VMD combined with ISSA to optimize LSSVM power prediction
- Database: Organize Four Practical SQL Server Scripting Functions
- IDEA中创建编写JSP
- 微软电脑管家2.0公测版体验
猜你喜欢
随机推荐
对产品设计,架构设计的一点思考
SegNet——论文笔记
JVM 快速检测死锁
DropBlock: 卷积层的正则化方法及复现代码
Visualization and Animation Technology (3D Visualization)
【音视频开发系列】fdk_aac 之 PCM 转 AAC
Flask request 返回网页中 checkbox 是否选中
matlab科研绘图模板,直接奉上源代码!
A semi-supervised Laplace skyhawk optimization depth nuclear extreme learning machine for classification
Microsoft computer butler 2.0 beta experience
YOLOv3详解:从零开始搭建YOLOv3网络
EfficientNet解读:神经网络的复合缩放方法(基于tf-Kersa复现代码)
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
GRNN、RBF、PNN、KELM之间究竟有什么联系?
异步编程之promise,任务队列,事件循环
手把手教你Charles抓包工具使用
自适应迁移学习核极限学习机用于预测
数据库文档生成工具V1.0
零分贝超静音无线鼠标!数量有限!!先到先得!!!【元旦专享】
set集合