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 null;
        }
        int len = 0;
        ListNode p = head;
        while(p != null) {
            p = p.next;
            len++;
        }
        int t = len - n;
        if(t == 0) {
            return head.next;
        }
        p = head;
        int i = 1;
        while(i < t) {
            i++;
            p = p.next;
        }
        p.next = p.next.next;
        return head;
    }
}