当前位置:网站首页>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;
}
};边栏推荐
- QT 出现多冲定义问题
- 布隆过滤器
- GRNN、RBF、PNN、KELM之间究竟有什么联系?
- RuntimeError: You called this URL via POST, but the URL doesn‘t end in a slash and you have APPEND_S
- 对象的扩展补充
- Visualization and Animation Technology (VR System)
- Detailed explanation of DenseNet and Keras reproduction code
- 如何用matlab做高精度计算?【第二辑】
- 叔本华的《人生的智慧》感悟
- MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
猜你喜欢

Hardware Knowledge: Introduction to RTMP and RTSP Traditional Streaming Protocols

用手机也能轻松玩转MATLAB编程

JVM工具之 JPS

狗都能看懂的CenterNet讲解及代码复现

Faster - RCNN principle and repetition code

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

VMD combined with ISSA to optimize LSSVM power prediction

Computer knowledge: desktop computers should choose the brand and assembly, worthy of collection

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

Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same
随机推荐
Unable to preventDefault inside passive event listener due to target being treated as passive. See
golang chan
什么是多态。
Computer software: recommend a disk space analysis tool - WizTree
异步编程之promise,任务队列,事件循环
代码小变化带来的大不同
VMD结合ISSA优化LSSVM功率预测
MAML principle explanation and code implementation
为什么不使用VS管理QT项目
Flask request 返回网页中 checkbox 是否选中
窥探晶体世界的奥秘 —— 230种空间群晶体结构模型全在这里
QT signals 保存到 QMap
MATLAB 的ICEEMDAN分解代码实现
DenseNet详解及Keras复现代码
Gramm Angle field GAF time-series data into the image and applied to the fault diagnosis
RHCE之路----全
NelSon:一款新的适配matlab编程语法的编程工具
自适应迁移学习核极限学习机用于预测
【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
字符串的一些方法