public ListNode removeNthFromEnd(ListNode head, int n) {
        if (n <= 0) return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        head = dummy;
        for (int i = 0; i < n; ++i) {
            head = head.next;
            if (head == null) return dummy.next;
        }
        ListNode prev = dummy;
        while (head.next != null) {
            head = head.next;
            prev = prev.next;
        }
        prev.next = prev.next.next;
        return dummy.next;
    }