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

舍不得花钱买1stOpt,不妨试试这款免费的拟合优化神器【openLU】

Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!

A priori box (Anchor) in target detection

华硕飞行堡垒系列无线网经常显示“无法连接网络” || 一打开游戏就断网

Operating System Random

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

电脑知识:台式电脑应该选择品牌和组装,值得收藏

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

狗都能看懂的Vision Transformer的讲解和代码实现

VMD combined with ISSA to optimize LSSVM power prediction
随机推荐
CMDB 阿里云部分实现
QT 显示窗口到最前面(非置顶)
A priori box (Anchor) in target detection
A semi-supervised Laplace skyhawk optimization depth nuclear extreme learning machine for classification
Error occurred while trying to proxy request项目突然起不来了
搭建redis哨兵
更改mysql数据库默认的字符集(mysql 存储 emoji表情)
MATLAB 的ICEEMDAN分解代码实现
MAML principle explanation and code implementation
SENet detailed explanation and Keras reproduction code
VMD combined with ISSA to optimize LSSVM power prediction
YOLOv3详解:从零开始搭建YOLOv3网络
目标检测中的IoU、GIoU、DIoU与CIoU
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
Logical Address & Physical Address
软件:给大家推荐一款国产非常好用的效率软件uTools
网络端口大全
Time Series Forecasting Based on Reptile Search RSA Optimized LSTM
unicloud 腾讯云 上传文件 Have no access right to the storage uniapp
JVM 快速检测死锁