当前位置:网站首页>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;
}
};边栏推荐
- [漏洞问题] log4j漏洞 关于2.17.0升级到2.18.0 方案
- matlab的2DCNN、1DCNN、BP、SVM故障诊断与结果可视化
- GRNN、RBF、PNN、KELM之间究竟有什么联系?
- Error occurred while trying to proxy request项目突然起不来了
- Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!
- Hardware Knowledge: Introduction to RTMP and RTSP Traditional Streaming Protocols
- Memory Management
- E-R图总结规范
- VS 2017编译 QT no such slot || 找不到*** 问题
- 网页中常用的两种绘图技术,用canvas绘图,绘制出一个三角形,矩形,柱状图,扇形图
猜你喜欢

Mac安装PHP开发环境

为什么不使用VS管理QT项目

Database knowledge: SQLServer creates non-sa user notes

QT QOpenGLWidget 全屏导致其他控件显示问题

基于爬行动物搜索RSA优化LSTM的时间序列预测

零分贝超静音无线鼠标!数量有限!!先到先得!!!【元旦专享】

Microsoft Store 微软应用商店无法连接网络,错误代码:0x80131500

unicloud 腾讯云 上传文件 Have no access right to the storage uniapp

格拉姆角场GAF将时序数据转换为图像并应用于故障诊断

【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
随机推荐
基于爬行动物搜索RSA优化LSTM的时间序列预测
golang 的库引用方法
天鹰优化的半监督拉普拉斯深度核极限学习机用于分类
HbuilderX 启动微信小程序 无法打开项目
更改mysql数据库默认的字符集(mysql 存储 emoji表情)
matlab的2DCNN、1DCNN、BP、SVM故障诊断与结果可视化
狗都能看懂的Vision Transformer的讲解和代码实现
RHCE之路----全
DenseNet详解及Keras复现代码
curl (7) Failed connect to localhost8080; Connection refused
如何用matlab做高精度计算?【第二辑】
Flask request 返回网页中 checkbox 是否选中
MySQL配置文件配置
Computer knowledge: desktop computers should choose the brand and assembly, worthy of collection
MAML principle explanation and code implementation
Visualization and Animation Technology (Computer Animation)
元素的增删克隆以及利用增删来显示数据到页面上
MySQL重置root密码
ES6新语法:symbol,map容器
Centos通过Docker搭建MySQL的PXC集群