题解 | #删除链表的倒数第n个节点#
删除链表的倒数第n个节点
http://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
C++代码:
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if (!head) {return NULL;}
vector<ListNode *>a;
while (head) {
a.push_back(head);
head = head->next;
}
if (a.size() == n) {return a[1];}
a[a.size() - n - 1]->next = a[a.size() - n + 1];
return a[0];
}
};