相当于删除正数第n个节点
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head) return head;
int listLength = 0;
ListNode* temp = head;
while(temp){
temp = temp->next;
++listLength;
}
int length = listLength - n + 1;
if(length == 1){
ListNode* temp = head;
head = head->next;
delete temp;
return head;
}
ListNode* pre = new ListNode(0, head);
temp = head;
while(length > 1){
pre = temp;
temp = temp->next;
--length;
}
pre->next = temp->next;
delete temp;
return head;
}
};
标签:pre,head,ListNode,temp,19,next,链表,length,倒数第 From: https://www.cnblogs.com/llllmz/p/18436513