当前位置:网站首页>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;
}
};边栏推荐
- ThreadLocal内存泄漏问题讲解
- 元素的增删克隆以及利用增删来显示数据到页面上
- 如何在Excel 里倒序排列表格数据 || csv表格倒序排列数据
- Microsoft Store 微软应用商店无法连接网络,错误代码:0x80131500
- 子空间结构保持的多层极限学习机自编码器(ML-SELM-AE)
- 【音视频开发系列】QT 采集麦克风PCM并播放
- HbuilderX 启动微信小程序 无法打开项目
- E-R图总结规范
- Interpretation of EfficientNet: Composite scaling method of neural network (based on tf-Kersa reproduction code)
- ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
猜你喜欢
随机推荐
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
如何用matlab做高精度计算?【第二辑】
QT 显示窗口到最前面(非置顶)
matlab的2DCNN、1DCNN、BP、SVM故障诊断与结果可视化
E-R图总结规范
花了近70美元入手的学生版MATLAB体验到底如何?
Based on the EEMD + + MLR GRU helped time series prediction
为什么不使用VS管理QT项目
Promise.all 使用方法
YOLOv3详解:从零开始搭建YOLOv3网络
软件稳定性思考
水平垂直居中的12种方法,任意插入节点的方法,事件的绑定的三种方法和解绑的方法,事件对象,盒子模型
Online public account article content to audio file practical gadget
mysql:列类型之float、double
网络端口大全
VS 2017编译 QT no such slot || 找不到*** 问题
Implementation of ICEEMDAN Decomposition Code in MATLAB
Microsoft computer butler 2.0 beta experience
【音视频开发系列】fdk_aac 之 PCM 转 AAC
代码小变化带来的大不同









