当前位置:网站首页>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;
}
};边栏推荐
猜你喜欢

自适应迁移学习核极限学习机用于预测

JVM 快速检测死锁

matlab的2DCNN、1DCNN、BP、SVM故障诊断与结果可视化

硬件知识:RTMP和RTSP传统流媒体协议介绍

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

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

VMD结合ISSA优化LSSVM功率预测

CMDB 腾讯云部分实现

VMD combined with ISSA to optimize LSSVM power prediction

2DCNN, 1DCNN, BP, SVM fault diagnosis and result visualization of matlab
随机推荐
核心价值观编码器【matlab版】
MySQL(4)
VS 2017编译 QT no such slot || 找不到*** 问题
基于时序模式注意力机制(TPA)的长短时记忆(LSTM)网络TPA-LSTM的多变量输入风电功率预测
目标检测中的先验框(Anchor)
RHCE之路----全
数据库技巧:整理SQLServer非常实用的脚本
Hardware Knowledge: Introduction to RTMP and RTSP Traditional Streaming Protocols
在线公众号文章内容转音频文件实用小工具
Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same
数据库:整理四个实用的SQLServer脚本函数
专属程序员的浪漫七夕
Scheduler (Long-term,Short-term, Medium-term Scheduler) & Dispatcher
狗都能看懂的Self-Attention讲解
this关键字,构造函数
系统流量预估、架构设计方案
基于EEMD+GRU+MLR的时间序列预测
unicloud 腾讯云 上传文件 Have no access right to the storage uniapp
窥探晶体世界的奥秘 —— 230种空间群晶体结构模型全在这里
为什么不使用VS管理QT项目