1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* dummy = new ListNode(0, head); ListNode* first = head; ListNode* second = dummy;
for (int i = 0; i < n; ++i) { first = first->next; }
while (first) { first = first->next; second = second->next; }
second->next = second->next->next;
ListNode* ans = dummy->next; delete dummy; return ans; } };
|