给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
示例1:
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例2:
输入:head = [1], n = 1
输出:[]
示例3:
输入:head = [1,2], n = 1
输出:[1]
链表中结点的数目为 sz
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
求出总长,然后倒数第n个就是正数第l-n+1个,那么循环遍历前面n个,当到达第n个时候,用下一个的下一个地址赋给第n个的地址,然后全部链表重新用ans存储,删除掉刚刚的临时链表node的空间。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int getLength(ListNode * head){
int length = 0;
while(head){
++length;
head = head->next;
}
return length;
}
ListNode* removeNthFromEnd(ListNode* head, int n) {
//初始化一个空节点,初始赋值为0,并且list的下一个next指针指向head,指针指向为list
ListNode * node = new ListNode(0,head);
ListNode * cur = node;
// cout << node->val;
// while(node != nullptr){
// length++;
// node = node->next; //不能写两遍
// }
int length = getLength(head);
for(int i = 1; i < length-n+1 ; i++){
cur = cur->next;
}
cur ->next= cur->next->next;
ListNode* ans = node->next;
delete node;
return ans;
}
};
遍历链表的同时将所有节点依次入栈。根据栈先进后出的原则,我们弹出栈的第n个节点就是需要删除的节点,并且目前栈顶的节点就是待删除节点的前驱节点。这个方法也是为了找到n所在的位置。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode * node = new ListNode(0, head);
ListNode * cur = node;
stack<ListNode *> stk;
while(cur){
stk.push(cur);
cur = cur->next;
}
for(int i = 0; i<n ; i++){
stk.pop();
}
ListNode * prev = stk.top();
prev->next = prev->next->next;
ListNode * ans = node->next;
delete node;
return ans;
}
};
初始时 \textit{first}first 和 \textit{second}second 均指向头节点。我们首先使用 \textit{first}first 对链表进行遍历,遍历的次数为 nn。此时,\textit{first}first 和 \textit{second}second 之间间隔了 n-1n−1 个节点,即 \textit{first}first 比 \textit{second}second 超前了 nn 个节点。
在这之后,我们同时使用 \textit{first}first 和 \textit{second}second 对链表进行遍历。当 \textit{first}first 遍历到链表的末尾(即 \textit{first}first 为空指针)时,\textit{second}second 恰好指向倒数第 nn 个节点。
根据方法一和方法二,如果我们能够得到的是倒数第 nn 个节点的前驱节点而不是倒数第 nn 个节点的话,删除操作会更加方便。因此我们可以考虑在初始时将 \textit{second}second 指向哑节点,其余的操作步骤不变。这样一来,当 \textit{first}first 遍历到链表的末尾时,\textit{second}second 的下一个节点就是我们需要删除的节点。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode * node = new ListNode(0, head);
ListNode * first = head;
ListNode * second = node;
for(int i = 0; i< n;i++){
first = first->next;
}
while(first){
first = first->next;
second = second->next;
}
second->next = second->next->next;
ListNode * ans = node->next;
delete node;
return ans;
}
};