struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x), next(next) {} }; #include class Solution { public: ListNode *removeNthFromEnd(ListNode *head, int n) { ListNode empty_node(0, head); ListNode *p = &empty_node; std::vector pv; while (p != nullptr) { pv.push_back(p); p = p->next; } p = pv[pv.size() - 1 - n]; p->next = p->next->next; return empty_node.next; } };