当前位置:网站首页>剑指 Offer 18. 删除链表的节点
剑指 Offer 18. 删除链表的节点
2022-06-30 05:23:00 【Grayson Zhang】
题目描述
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
注意:此题对比原题有改动
示例 1:
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:
输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
说明:
题目保证链表中节点的值互不相同
若使用 C 或 C++ 语言,你不需要 free 或 delete 被删除的节点
题解:
1. python
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode()
dummy.next = head
cur = dummy
while cur.next:
if cur.next. val == val:
cur.next = cur.next.next
return dummy. next
cur = cur. next
else:
return -1

2. C++
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;
}
};

边栏推荐
- Read and save txt files
- Chapter 10 of OpenGL super classic (7th Edition) calculation shader
- Unity3d get screen width and height
- 炒美原油的国际交易平台如何能保障资金安全呢?
- Unity + hololens common basic functions
- Access is denied encountered when vfpbs calls excel under IIS
- 网络变压器怎么判断好坏?网络滤波变压器坏了一般是什么症状?
- [typescript] cannot redeclare block range variables
- 图扑软件基于钻孔数据的三维地质模型可视化
- 【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
猜你喜欢

Records of problems encountered in unity + hololens development

【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)

网络变压器怎么判断好坏?网络滤波变压器坏了一般是什么症状?

Use the code cloud publicholiday project to determine whether a day is a working day

14x1.5cm vertical label is a little difficult, VFP calls bartender to print

Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation

PWN入门(2)栈溢出基础

Unity scroll view element drag and drop to automatically adsorb centering and card effect

Introduction to mmcv common APIs

Unityshader learning notes - Basic Attributes
随机推荐
Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself
Unity notes_ SQL Function
Use the code cloud publicholiday project to determine whether a day is a working day
【 VCS + Verdi joint simulation】 ~ Taking Counter as an Example
[learning notes] AssetBundle, xlua, hot update (use steps)
Unity packaging and publishing webgl error reason exception: failed building webgl player
Virtual and pure virtual destructions
Pyinstaller flash back
Unity call Exe program
Unity camera control
Pytorchcnn image recognition and classification model training framework
Redistemplate common method summary
【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)
GoLand No Tests Were Run : 不能使用 fmt.Printf() <BUG>
Tensorflow2 of ubantu18.04 X installation
Revit二次開發---未打開項目使用面板功能
Another download address for typro
unity 扫描圈 圆扩展方法
旋转框目标检测mmrotate v0.3.1 学习配置
C # uses monopinvokecallback to directly call back C # function