class Solution {
    // 翻转再删除再翻转
  public:
    ListNode* reverseList(ListNode* head) {
        if (head == nullptr)return head;
        ListNode* front = nullptr, *cur = head;
        while (cur) {
            ListNode* nxt = cur->next;
            cur->next = front;
            front = cur;
            cur = nxt;
        }
        return front;
    }
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        head = reverseList(head);
        ListNode* cur = head->next, *front = head;
        n--;
        if (!n) {
            head = cur;
        } else {
            while (--n) {
                front = cur;
                cur = cur->next;
            }
            front->next = cur->next;
        }
        head = reverseList(head);
        return head;
    }
};