当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
电脑知识:台式电脑应该选择品牌和组装,值得收藏
如何用matlab做高精度计算?【第三辑】(完)
华硕飞行堡垒系列无线网经常显示“无法连接网络” || 一打开游戏就断网
Faster - RCNN principle and repetition code
Time Series Forecasting Based on Reptile Search RSA Optimized LSTM
子空间结构保持的多层极限学习机自编码器(ML-SELM-AE)
MySQL面试题大全(陆续更新)
matlab封闭曲线拟合 (针对一些列离散点)
matlab的2DCNN、1DCNN、BP、SVM故障诊断与结果可视化
EfficientNet解读:神经网络的复合缩放方法(基于tf-Kersa复现代码)
随机推荐
如何用matlab做高精度计算?【第二辑】
RHCE之路----全
QT QOpenGLWidget 全屏导致其他控件显示问题
SENet详解及Keras复现代码
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
golang chan
Hardware Knowledge: Introduction to RTMP and RTSP Traditional Streaming Protocols
Database knowledge: SQLServer creates non-sa user notes
Microsoft computer butler 2.0 beta experience
天鹰优化的半监督拉普拉斯深度核极限学习机用于分类
华硕飞行堡垒系列无线网经常显示“无法连接网络” || 一打开游戏就断网
MAML原理讲解和代码实现
【音视频开发系列】QT 采集麦克风PCM并播放
软件:给大家推荐一款国产非常好用的效率软件uTools
异步编程之promise,任务队列,事件循环
NelSon:一款新的适配matlab编程语法的编程工具
电脑软件:推荐一款磁盘空间分析工具——WizTree
Scheduler (Long-term,Short-term, Medium-term Scheduler) & Dispatcher
解决腾讯云DescribeInstances api查询20条记录以上的问题
[漏洞问题] log4j漏洞 关于2.17.0升级到2.18.0 方案