import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode removeNthFromEnd (ListNode head, int n) {
        // write code here
        if(head == null) return head;
        ListNode empHead = new ListNode(-1);
        empHead.next = head;
        ListNode fastNode = empHead;
        ListNode slowNode = empHead;
        ListNode preNode = empHead;
        for(int i = 0; i < n; i++){
            fastNode = fastNode.next;
        }
        while(fastNode != null){
            preNode = slowNode;
            fastNode = fastNode.next;
            slowNode = slowNode.next;
        }
        preNode.next = slowNode.next;
        return empHead.next;
    }
}