题目地址:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
解题思路:题目要求一次遍历,设置双指针,通过第二个指针来判断是否第一个指针到达倒数第n个。
class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode *p, *q; p = q = head; for (int i = 0; i < n ; i++) p = p->next; if(p==NULL) return head->next; while (p->next != NULL) { q = q->next; p = p->next; } q->next = q->next->next; return head; } };