当前位置:网站首页>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;
}
};边栏推荐
- 七夕专属程序员的浪漫
- Database document generation tool V1.0
- 异步编程之promise,任务队列,事件循环
- SENet detailed explanation and Keras reproduction code
- 用手机也能轻松玩转MATLAB编程
- 【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
- 什么是多态。
- EfficientNet解读:神经网络的复合缩放方法(基于tf-Kersa复现代码)
- MySQL重置root密码
- 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)解决办法

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

YOLOv3详解:从零开始搭建YOLOv3网络

MySQL面试题大全(陆续更新)

A priori box (Anchor) in target detection

matlab封闭曲线拟合 (针对一些列离散点)

Computer software: recommend a disk space analysis tool - WizTree

如何用matlab做高精度计算?【第三辑】(完)

Faster - RCNN principle and repetition code
随机推荐
Faster RCNN原理及复现代码
IDEA 控制台 中文乱码问题(如果网上教程都无法解决你的问题的话)
对象的扩展补充
curl (7) Failed connect to localhost8080; Connection refused
MySQL(4)
set集合
Database document generation tool V1.0
数据库技巧:整理SQLServer非常实用的脚本
VS 2017编译 QT no such slot || 找不到*** 问题
狗都能看懂的CenterNet讲解及代码复现
NelSon:一款新的适配matlab编程语法的编程工具
JVM调优实践
Nacos 原理
如何用matlab做高精度计算?【第二辑】
ES6新语法:symbol,map容器
手把手教你Charles抓包工具使用
SENet详解及Keras复现代码
误差指标分析计算之matlab实现【开源1.0.0版】
【C# - 方法封装】数据转换
Hardware Knowledge: Introduction to RTMP and RTSP Traditional Streaming Protocols