当前位置:网站首页>Sword finger offer 18 Delete the node of the linked list
Sword finger offer 18 Delete the node of the linked list
2022-06-30 05:31:00 【Grayson Zhang】
Title Description
Given the head pointer of one-way linked list and the value of a node to be deleted , Define a function to delete the node .
Return the head node of the deleted linked list .
Be careful : This question is different from the original one
Example 1:
Input : head = [4,5,1,9], val = 5
Output : [4,1,9]
explain : Given that the value of your list is 5 Second node of , So after calling your function , The list should be 4 -> 1 -> 9.
Example 2:
Input : head = [4,5,1,9], val = 1
Output : [4,5,9]
explain : Given that the value of your list is 1 The third node of , So after calling your function , The list should be 4 -> 5 -> 9.
explain :
Ensure that the values of nodes in the list are different from each other
If you use C or C++ Language , You don't need to free or delete Deleted node
Answer key :
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;
}
};

边栏推荐
- Unity scroll view element drag and drop to automatically adsorb centering and card effect
- Very nervous. What should I do on the first day of software testing?
- Unity ugui text value suspended enlarged display add text background
- Video summary of my station B
- Unity C trigonometric function, right triangle corner calculation
- Redistemplate common method summary
- How to prevent source code leakage in enterprises and institutions
- Unity 3D model operation and UI conflict Scrollview
- Unityshader learning notes - Basic Attributes
- Unity screenshot method
猜你喜欢

Vfpbs uploads excel and saves MSSQL to the database

Super comprehensive summary | related improvement codes of orb-slam2 / orb-slam3!

Unity- the camera follows the player

How to prevent source code leakage in enterprises and institutions

Unity 3D model operation and UI conflict Scrollview

2022年,谁在推动音视频产业的新拐点?

The fourth day of learning C language for Asian people

Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself

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

Sound net, debout dans le "sol" de l'IOT
随机推荐
Expansion method of unity scanning circle
Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself
14x1.5cm竖向标签有点难,VFP调用BarTender来打印
Array pointers and pointer arrays
Unity call Exe program
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
86. 分隔链表
Virtual and pure virtual destructions
Unity scroll view element drag and drop to automatically adsorb centering and card effect
Operation of JSON file
AI大模型落地大考,浪潮交出了怎样的答卷?
Learning about functions QAQ
The definition of strain was originally from stretch_ Ratio started
Rotating frame target detection mmrotate v0.3.1 training dota data set (II)
E: Topic focus
UML tools
声网,站在物联网的“土壤”里
How to use js to control the scroll bar of moving div
East Tower attack and defense world - XSS bypasses the safety dog
Database SQL language 04 subquery and grouping function