public static ListNode removeNthFromEnd(ListNode head, int n) {//accepted
        if (head == null) {
            return head;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode p = head;
        while (n > 0) {
            p = p.next;
            n--;
        }
        ListNode p2 = dummy;
        while (p != null) {
            p = p.next;
            p2 = p2.next;
        }
        if (p2 != null && p2.next != null) {
            p2.next = p2.next.next;
        }
        return dummy.next;
    }