public ListNode removeNthFromEnd(ListNode head, int n) {
        if (n <= 0) {
            return head;
        }
        ListNode first = head;

        while (head != null && n > 0) {
            head = head.next;
            n--;
        }
        if (n != 0) {
            return first;
        }

        ListNode cur = new ListNode(0);
        cur.next = first;
        while (head != null) {
            head = head.next;
            cur = cur.next;
        }
        //remove cur
        if (cur.next == first) {
            return cur.next.next;
        } else {
            cur.next = cur.next.next;
            return first;
        }

    }