1. 链表赋值时,左边一定是被赋值的变量,否则程序运行不报错,但是会超出时间限制!
注意:一定要看清楚,把谁赋值给谁,把A赋值给B:B=A
左边一定是B,右边一定是A
// leetcode19题: 删除链表的倒数第 N 个结点
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyhead = new ListNode(0);
dummyhead->next = head;
ListNode* first = dummyhead;
ListNode* last = dummyhead;
while(n--){
first = first->next;
//first->next = first; 不会报错,但是会超时
}
while(first->next != nullptr){
first = first->next;
//first->next = first; 不会报错,但是会超时
last = last->next;
//last->next = last; 不会报错,但是会超时
}
ListNode* temp = last->next;
last->next = last->next->next;
delete temp;
return dummyhead->next;
}
};