struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode dummy(-1);
dummy.next = head;
ListNode *fast = &dummy;
ListNode *slow = &dummy;
for(int i = 0; i < n; i++){ //移动[n]步
fast = fast->next;
}
while(fast->next != NULL){ //移动[链表长度-n]步
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummy.next;
}
};
[LeetCode] 19. Remove Nth Node From End of List
原文:https://www.cnblogs.com/wengle520/p/12316769.html