当前位置:网站首页>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;
}
};
边栏推荐
- 对象的扩展补充
- 七夕专属程序员的浪漫
- 数组的一些方法
- Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same
- Microsoft Store 微软应用商店无法连接网络,错误代码:0x80131500
- 字符串的一些方法
- U-Net详解:为什么它适合做医学图像分割?(基于tf-Kersa复现代码)
- Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!
- MAML principle explanation and code implementation
- SENet详解及Keras复现代码
猜你喜欢
【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
SegNet——论文笔记
如何用matlab做高精度计算?【第二辑】
VMD结合ISSA优化LSSVM功率预测
如何在Excel 里倒序排列表格数据 || csv表格倒序排列数据
Interpretation of EfficientNet: Composite scaling method of neural network (based on tf-Kersa reproduction code)
数据库:整理四个实用的SQLServer脚本函数
HbuilderX 启动微信小程序 无法打开项目
窥探晶体世界的奥秘 —— 230种空间群晶体结构模型全在这里
QT 出现多冲定义问题
随机推荐
如何用matlab做高精度计算?【第三辑】(完)
mysql锁机制
IoU, GIoU, DIoU and CIoU in target detection
CMDB 阿里云部分实现
MySQL配置文件配置
数据库:整理四个实用的SQLServer脚本函数
Jenkins pipeline 自动部署实践
VMD combined with ISSA to optimize LSSVM power prediction
MAML principle explanation and code implementation
零分贝超静音无线鼠标!数量有限!!先到先得!!!【元旦专享】
目标检测中的先验框(Anchor)
Visualization and Animation Technology (3D Visualization)
异步编程之promise,任务队列,事件循环
mysql:列类型之float、double
Mac安装PHP开发环境
如何画好业务架构图。
Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same
unicloud 腾讯云 上传文件 Have no access right to the storage uniapp
狗都能看懂的Pytorch MAML代码详解
MySQL(4)