/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *  ListNode(int x) :val(x), next(NULL) {}
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        // write code here
        ListNode* hail = new ListNode(0);
        hail->next = head;
        
        ListNode* slow = hail;
        ListNode* fast = hail;
        
        for(int i=0; i<n; i++){
            fast = fast->next;
        }
        
        while(fast != nullptr && fast->next != nullptr){
            slow = slow->next;
            fast = fast->next;
        }
        
        slow->next = slow->next->next;
        return hail->next;
    }
};