当前位置:网站首页>剑指 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;
}
};

边栏推荐
- 东塔攻防世界—xss绕过安全狗
- Unity 3D model operation and UI conflict Scrollview
- Bessel curve with n control points
- QT connecting external libraries
- Intellj idea jars projects containing external lib to other project reference methods - jars
- Chapter 11 advanced data management of OpenGL super classic (version 7)
- [note] usage model tree of the unity resource tree structure virtualizingtreeview
- 3D rotation album
- Unity camera control
- Unity notes_ SQL Function
猜你喜欢

ParticleSystem in the official Manual of unity_ Collision module

Unity project hosting platform plasticscm (learn to use 1)

Redistemplate common method summary

mmcv常用API介绍

Unity project hosting platform plasticscm (learn to use 2)

网络变压器怎么判断好坏?网络滤波变压器坏了一般是什么症状?
![[note] usage model tree of the unity resource tree structure virtualizingtreeview](/img/3e/fe5610c797a14554ad735172c3ab54.jpg)
[note] usage model tree of the unity resource tree structure virtualizingtreeview
![[typescript] cannot redeclare block range variables](/img/52/2fd3071ca9e3c5023c6b65961e2cf7.jpg)
[typescript] cannot redeclare block range variables

Win10 vs2015 compiling curaengine

Use the code cloud publicholiday project to determine whether a day is a working day
随机推荐
Unity3d- use animator and code to control task walking
mmdet之Loss模块详解
Unity screenshot method
炒美原油的国际交易平台如何能保障资金安全呢?
Golden code of programmer interview
【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
Ugui uses its own function to realize reverse mask
14x1.5cm vertical label is a little difficult, VFP calls bartender to print
Writing unityshader with sublimetext
Unity application class and data file path
Unity C trigonometric function, right triangle corner calculation
[Motrix] download Baidu cloud files using Motrix
[typescript] defines the return value type of promise
中文版PyCharm改为英文版PyCharm
Postman 做测试的 6 个常见问题
Unity3d learning notes-1 (C # learning)
2021-06-17 solve the problem of QML borderless window stretching, window jitter and flicker when stretching and shrinking
Unity limited time use limited trial time and use times
Nestjs introduction and environment construction
ParticleSystem in the official Manual of unity_ Collision module