首页 > 代码库 > LeetCode "Remove Nth Node From End of List"

LeetCode "Remove Nth Node From End of List"

Another Double pointer solution. 1A!

class Solution {public:    ListNode *removeNthFromEnd(ListNode *head, int n) {                ListNode *p = NULL;        ListNode *p0 = head;        ListNode *p1 = head;        ListNode *pre1 = NULL;        while (--n)        p0 = p0->next;                while (p0->next)        {            p0 = p0->next;            pre1 = p1;            p1 = p1->next;        }        if (p1 == head) p = head->next;        else        {            pre1->next = p1->next;            p = head;        }        return p;    }};