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;
        ListNode preHead = new ListNode(0);
        ListNode preHead2 = preHead;
        preHead.next = head;
        ListNode index = head;
        int len = 0;
        while (index != null) {
            index = index.next;
            len++;
        }
        if (n > len) return null;
        int length = len - n;
        while (length != 0) {
            preHead = preHead.next;
            head = head.next;
            length--;
        }

        preHead.next = head.next;


        return preHead2.next;
    }
}