1 快慢指针
代码语言:javascript复制class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
auto pre = new ListNode();
auto prehead = pre;
pre->next = head;
auto slow = head;
auto fast = head;
for (int i = 0; i < n; i )
fast = fast->next;
while (fast) {
slow = slow->next;
pre = pre->next;
fast = fast->next;
}
pre->next = slow->next;
return prehead->next;
}
};