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

struct ListNode 
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};


ListNode *RemoveNthFromEnd(ListNode *head, int n) 
{
    ListNode * p = head;
    while(n-- > 0 && p != NULL)
        p=p->next;

    //here is a pointer to ListNode point
    ListNode ** remove = &head;

    while(p != NULL)
    {
        p = p->next;
        remove = &((*remove)->next);
    }

    //set pointer to next to remove the node
    *remove = (*remove)->next;

    return head;        
}
View Program Text


Test Status