/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

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