Given a linked list, remove the nth node from the end of list and return its head.
For example
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
移除链表从结尾起的第n个结点,并返回移除后的链表的第一个结点。
题目给出的声明:第一个参数是链表的第一个结点
ListNode* removeNthFromEnd(ListNode* head, int n);
思路1:
题目相当于是删除从头起的第L-n+1个结点,其中L是链表长度。但是长度不知道,所以首先求链表长。然后就可以按照常规的方法来处理了:
需要注意的是,这里最好加一个额外的辅助头结点,指向链表的第一个结点。当要删除的结点恰好是链表的第一个结点时,这样处理会简化很多。
没有辅助头结点:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int NodeCount = 0;
ListNode *firstNode = head;//第一个结点
while (firstNode)
{
++NodeCount;
firstNode = firstNode->next;
}
if (n > NodeCount) return NULL;
int removeIndex = NodeCount - n;
ListNode *pre = head;
if (removeIndex)
{
for (int i = 0; i < removeIndex-1; i++)
{
pre = pre->next;
}
ListNode *q = pre->next;
pre->next = q->next;
delete q;
return head;
}
else
{
ListNode *newHead = head->next;
delete head;
return newHead;
}
}
有辅助头结点:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummy = new ListNode(0);//头结点
dummy->next = head;
int nodeCount = 0;
ListNode *p = head;//指向第一个结点
while (p)
{
++nodeCount;
p = p->next;
}
if (n > nodeCount) return NULL;
int removeIndex = nodeCount - n;
p = dummy;//指向头结点
//找到被删结点的前一个位置
for (int i = 0; i < removeIndex; i++)
{
p = p->next;
}
ListNode *q = p->next;
p->next = q->next;
delete q;
return dummy->next;
}
思路2:快慢指针
快慢指针初始都是从辅助头结点开始,先让快指针跑n+1次,然后慢指针开始和快指针一起跑,此时快慢指针之间正好有n个结点。当快指针到达链表末尾(已指向NULL),慢指针正好指向要删除的结点的前一个结点。接下来的工作就和思路1的图示一样了。
这种方法一次迭代即可。
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummy = new ListNode(0);//头结点
dummy->next = head;
ListNode *fast = dummy, *slow = dummy;
for (int i = 0; i < n+1; ++i)
{
fast = fast->next;
}
while (fast)
{
fast = fast->next;
slow = slow->next;
}
ListNode *delNode = slow->next;
slow->next = delNode->next;
delete delNode;
return dummy->next;
}
改进:使用二级指针删除单链表结点
通常会定义一个prev指针来保存被删除节点的前一节点的信息。利用二级指针的特性,prev就不需要了,也不需要像使用一级指针那样先找到被删结点的前一结点。
|1|-| --> |2|-| --> |3|-| --> NULL
ListNode **del = &node2;
*del = (*del)->next;
第二个结点就被删除掉了|1|-| --> |3|-| --> NULL
可以这样理解,一级指针操作的是结点本身,而二级指针操作的是指向结点的指针。使用二级指针就不需要再添加辅助的头结点了。
删除第n个结点
ListNode* del_node(ListNode* head, int n){
ListNode **pp = &head;
for (int i = 0; i < n; ++i) {
pp = &((*pp)->next);
}
ListNode *p = *pp;
*pp = (*pp)->next;
delete p;
return head;
}
完整代码:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *createList(initializer_list<int> list) {
ListNode *head = new ListNode(0), *cur = head;
if (list.size())
{
for (auto beg = list.begin(); beg != list.end(); ++beg)
{
cur->next = new ListNode(*beg);
cur = cur->next;
}
}
return head->next;
}
class Solution{
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode **pp = &head;
ListNode *fast = head;
for (int i = 0; i < n; ++i) {
fast = fast->next;
}
while (fast) {
fast = fast->next;
pp = &((*pp)->next);
}
ListNode *delNode = *pp;
*pp = (*pp)->next;
delete delNode;
return head;
}
};
int main(int argc, const char * argv[]) {
ListNode *l1 = createList({1,2,3,4});
Solution s;
ListNode *result = s.removeNthFromEnd(l1, 2);
for (ListNode *cur = result; cur != NULL; cur = cur->next)
{
cout << cur->val << "->";
}
return 0;
}