当前位置:网站首页>leetcode 19. Delete the penultimate node of the linked list
leetcode 19. Delete the penultimate node of the linked list
2022-06-27 16:59:00 【chenyson】
difficulty : secondary
The frequency of :75
problem : I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list 
** Their thinking :** Double pointer , Just navigate to the node to be deleted and the previous node , You can delete
Be careful :
- Last but not least n Nodes , You can use a tail Pointer first n Step
- then tail and cur Went together , until tail Get to the last step
- This is the time cur Is the node to be deleted
- pre To locate the node before the node to be deleted
- Because there may only be one node , It will be empty after deletion , This situation needs to be written out in advance
Code :
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
// The first node has active , So you need a virtual head node
ListNode dummyhead=new ListNode(0);
dummyhead.next=head;
// Double pointer , Fixed distance pointer moves together
ListNode tail=head,pre=dummyhead,cur=head;
// When there is only one node , Delete must be for null
if(head.next==null) return null;
// Last but not least n individual , Last but not least n Moved to the last one n-1 individual
for(int i=0;i<n-1;i++){
tail=tail.next;
}
// The two pointers move together , When tail When you reach the last node , The location you just reached is the node you want to delete
while(tail.next!=null){
//pre Is used to mark the previous node of the deleted node
pre=cur;
cur=cur.next;
tail=tail.next;
}
// Delete cur node
pre.next=cur.next;
return dummyhead.next;
}
}
边栏推荐
- EMQ helps Qingdao Yanbo build a smart water platform
- Oracle concept 3
- Byte + Google super full kotlin learning King fried notes! Kotlin introductory tutorial + Advanced kotlin enhanced actual combat (with demo)
- C语言集合运算
- Deeply digitise, lead cloud nativity and serve more developers
- Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
- d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
- Simulated process scheduling
- Event listening mechanism
- d3dx9_ 39.dll how to repair -d3dx9_ 39.dll missing file download
猜你喜欢
![[the way of programmer training] - 3 Character count statistics](/img/0c/e506571a4b22edc7c02a346909eeab.jpg)
[the way of programmer training] - 3 Character count statistics

EMQ 助力青岛研博建设智慧水务平台

Annual comprehensive analysis of China's audio market in 2022

Leetcode 33. Search rotation sort array

【多线程】线程通信调度、等待集 wait() 、notify()

Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'

Four characteristics of transactions

Missing d3d10 How to repair DLL files? Where can I download d3d10.dll

Leetcode 46 Full Permutation

Sliding window + monotone queue concept and example (p1886 Logu)
随机推荐
09 route guard authenticates URL
Annual comprehensive analysis of China's audio market in 2022
[the way of programmer training] - 3 Character count statistics
Hierarchical clustering and case analysis
C语言集合运算
Leetcode 46 Full Permutation
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
d3dx9_ How to repair 33.dll? d3dx9_ What if 33.dll is lost?
Halcon: discrete digital OCR recognition
Popularization of MCU IO port: detailed explanation of push-pull output and open drain output
Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration
等保三级密码复杂度是多少?多久更换一次?
Simulated process scheduling
ROS "topic" programming implementation
2/14 preliminary calculation geometry
C语言教师工作量管理系统
#yyds干货盘点#简述chromeV8引擎垃圾回收
Determine the maximum number of specific words in a string
Unity 阴影——阴影平坠(Shadow pancaking)