当前位置:网站首页>Leetcode-19- delete the penultimate node of the linked list (medium)
Leetcode-19- delete the penultimate node of the linked list (medium)
2022-06-13 01:03:00 【Didi dada】
19. Delete the last of the linked list N Nodes ( secondary )
I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list .
Advanced : Can you try a scan implementation ?
Example 1:

Input :head = [1,2,3,4,5], n = 2
Output :[1,2,3,5]
Example 2:
Input :head = [1], n = 1
Output :[]
Example 3:
Input :head = [1,2], n = 1
Output :[1]
Ideas :
Set a dummy node , its next The pointer points to the head node of the list . such , There is no need to make special judgment on the head node .
for example , In the subject , If we want to delete nodes y, We need to know the nodes y The precursor node of x, And will x Pointer to y Successor node . But because there is no precursor node in the head node , So we need to make a special judgment when deleting the head node . But if we add dummy nodes , Then the precursor of the head node is the dummy node itself , At this point, we just need to consider the general situation .
- Two iterations : The first traversal determines the length of the linked list L,( set up head The node is the first node ), Is the first
L-n+1Nodes are nodes to be deleted , The firstL-nNodes are their precursor nodes . then , The second traversal finds the secondL-nNodes , And make it next Point tol-n+2Nodes . - Stack : According to the nature of the stack , Stack all nodes , Is the first n The nodes that are out of the stack are the nodes to be deleted , When it comes out of the stack , The precursor node is
stack.top()Node to , Set to temp, Make it next Point totemp->next->nextThe node of . - Double pointer : Set double pointer i,j, send j Pointer ratio i Pointer fast n Step , be j Pointer to NULL when ,i The pointer points to the node to be deleted ; if j Pointer ratio i Pointer fast n+1 Step , be j Pointer to NULL when ,i The pointer points to the precursor node of the node to be deleted , Make it next Point to
temp->next->nextThe node of .( Traverse once )
(1)C++_1
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* p = new ListNode(0,head);
ListNode* q = head;
int l = 0;
while(q!=nullptr){
l+=1;
q = q->next;
}
ListNode* temp = p;
for(int i=0;i<l-n;i++){
temp = temp->next;
}
temp->next=temp->next->next;
ListNode* res = p->next;
delete p;
return res;
}
};
(2)C++_2
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* p = new ListNode(0,head);
ListNode* q = p;
stack<ListNode*> s;
while(q!=nullptr){
s.push(q);
q=q->next;
}
for(int i=0;i<n;i++){
s.pop();
}
ListNode* temp = s.top();
temp->next = temp->next->next;
ListNode* res = p->next;
delete p;
return res;
}
};
(3)C++_3
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* p = new ListNode(0,head);
ListNode* i = p;
ListNode* j = head;
for(int k=0;k<n;k++){
j=j->next;
}
while(j!=nullptr){
j=j->next;
i=i->next;
}
i->next = i->next->next;
ListNode* res = p->next;
delete p;
return res;
}
};
(4)python_1
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
p = ListNode(0,head)
l=0
q = head
while(q!=None):
l+=1
q=q.next
temp = p
for i in range(l-n):
temp = temp.next
temp.next=temp.next.next
res = p.next
del p
return res
(5)python_2
python Available in the list Realize the function of stack
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
p = ListNode(0,head)
s=[]
q = p
while(q!=None):
s.append(q)
q=q.next
for i in range(n):
s.pop()
temp = s[-1]
temp.next=temp.next.next
res = p.next
del p
return res
(6)python_3
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
p = ListNode(0,head)
i = p
j = head
for k in range(n):
j = j.next
while(j!=None):
j = j.next
i = i.next
i.next=i.next.next
res = p.next
del p
return res
边栏推荐
- Expression tree - medium order printout
- Matrix fast power
- Kotlin collaboration, the life cycle of a job
- [imx6ull] video monitoring project (USB camera +ffmepeg)
- Comparison of disk partition modes (MBR and GPT)
- . The way to prove the effect of throwing exceptions on performance in. Net core
- 【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
- AOF持久化
- [JS component] custom paging
- Pipeline pipeline project construction
猜你喜欢

五篇经典好文,值得一看
![[JS component] floating text](/img/e5/7faad5422bba919bed34e3dbcf7ba0.jpg)
[JS component] floating text

Status of the thread

(01).NET MAUI实战 建项目

Expression tree - medium order printout

Introduction to ROS from introduction to mastery (zero) tutorial

Canvas game 2048 free map size

Most elements leetcode

Self use notes for problem brushing learning

Win10 home vs pro vs enterprise vs enterprise LTSC
随机推荐
Et5.0 simply transform referencecollectorieditor
Binary tree -- using hierarchical sequence and middle sequence to determine a tree
Status of the thread
Leetcode-13- Roman numeral to integer (simple)
单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
pytorch是什么?解释pytorch的基本概念
About_ int128
Remove duplicates from an ordered array
.net core 抛异常对性能影响的求证之路
Jenkins continuous integration operation
什么是 Meebits?一个简短的解释
How to handle different types of data
[North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
Androi weather
[backtrader source code analysis 7] analysis of the functions for calculating mean value, variance and standard deviation in mathsupport in backtrader (with low gold content)
Pipeline流水线项目构建
Higherhrnet pre training model -- download from network disk
[latex] insert picture
Unity extension
[server data recovery] successful cases of data loss recovery during data migration between storage servers