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
        ListNode pre = new ListNode(-1);
        pre.next = head;
        ListNode next = head;
        ListNode result = pre;
        while (n-->0){
            next = next.next;
        }
        while (next!=null){
            pre = pre.next;
            head = head.next;
            next = next.next;
        }
        //删除
        pre.next = head.next;
        //返回头节点
        return result.next;
    }
}