0019.cpp (399B)
1 class Solution { 2 public: 3 ListNode *removeNthFromEnd(ListNode *head, int n) { 4 ListNode *b = head; 5 6 n++; 7 for (ListNode *a = head; a; a = a->next) { 8 if (!n) 9 b = b->next; 10 else 11 n--; 12 } 13 if (n) 14 head = head->next; 15 else 16 b->next = b->next->next; 17 return head; 18 } 19 };