当前位置:网站首页>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;
}
};
边栏推荐
- Computer software: recommend a disk space analysis tool - WizTree
- Detailed explanation of DenseNet and Keras reproduction code
- 在线公众号文章内容转音频文件实用小工具
- Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
- MySQL配置文件配置
- 硬件知识:RTMP和RTSP传统流媒体协议介绍
- Operating System Kernel
- ffmpeg打开rtsp流应该设置的几个参数
- 格拉姆角场GAF将时序数据转换为图像并应用于故障诊断
- VMD combined with ISSA to optimize LSSVM power prediction
猜你喜欢
随机推荐
ThreadLocal内存泄漏问题讲解
软件:给大家推荐一款国产非常好用的效率软件uTools
SENet详解及Keras复现代码
Database knowledge: SQLServer creates non-sa user notes
Computer software: recommend a disk space analysis tool - WizTree
mysql月份比較是否相等
Visualization and Animation Technology (Computer Animation)
curl (7) Failed connect to localhost8080; Connection refused
核心价值观编码器【matlab版】
Database: Organize Four Practical SQL Server Scripting Functions
U-Net详解:为什么它适合做医学图像分割?(基于tf-Kersa复现代码)
Centos通过Docker搭建MySQL的PXC集群
【音视频开发系列】fdk_aac 之 PCM 转 AAC
Implementation of ICEEMDAN Decomposition Code in MATLAB
golang 坐标格式 转换 GCJ02ToWGS84
代码小变化带来的大不同
异步编程之promise,任务队列,事件循环
MySQL(4)
SegNet——论文笔记
Jenkins pipeline 自动部署实践