# My short C++ solution

• ``````class Solution
{
public:
{
for(int i = 1; i < n; ++i)
{
t2 = t2->next;
}
while(t2->next != NULL)
{
t1 = &((*t1)->next);
t2 = t2->next;
}
*t1 = (*t1)->next;
}
};``````

• what a clever and elegant code it is ！！！

• Really smart!

• great solution!

• I see a memory leak...

• could anyone give some explanation? thx

• unbelivable!!!!!!!

• you didn't delete the node, it is still in the heap

• basically you need a pointer (call it p1) iterate through the first n terms in the list(try to think it as an offset), then you keep iterate the p1, when it reach to the end, your second pointer call it p2, will be at the exact nth position from the end.

• what if the list has only one node and n == 1?
then
while(t2->next != NULL)
will cause a runtime error.

• See the problem note

• Your solution is very smart!!! But the double pointer let me difficult to understand. So I rewrite you solution.

``````ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* ans;

for(int i=0;i<n;++i)
{
fast = fast->next;
}

if(fast == NULL)
{
return ans;
}

while(fast->next != NULL)
{
fast = fast->next;
slow = slow->next;
}
delete slow->next;
slow->next = slow->next->next;

}
``````

• Thanks for sharing.
Here is a recursive solution.

code:

``````int countAndRemove(struct ListNode *node, int n){

//Once the stack frame reaches the tail, counting starts.
if(!node->next) return n-1;

int NumOfNodesLeft = countAndRemove(node->next, n);

//If there are exactly n nodes in the rest of the list, delete next node.
if(NumOfNodesLeft  == 0)  node->next = (node->next)->next;

//Count decremented.
return NumOfNodesLeft - 1;
}

struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
}
``````

The idea is based on how function call, namely pushing and popping stack frames, works.

``````i) Push as many stack frames as the size of linked list. so n is passed to the end (top frame).
ii) As the last frame pops off, counting begins.
``````

Suppose there S nodes in the linked list.

The stack frames would be like the following:

===================================================

Sth stack: 1st node ( counting from the back ) n = n, NumOfNodesLeft = n, return n - 1.

===================================================

(S-1)th stack: 2nd node ( from the back ), n = n, NumOfNodesLeft = n - 1, return n - 2.

===================================================

:

===================================================

(S-n-1)th stack: 2nd node ( from the back ), n = n, NumOfNodesLeft = 0, return -1.

Only this frame does the deletion.

===================================================

:

===================================================

Second stack: (S-1)Th node ( counting from the back ), NumOfNodesLeft = n - (S -1), return n - (S-1) - 1.

===================================================

First stack: Sth node from the back / 1st node in the list, NumOfNodesLeft = n - S, return n - S - 1.

===================================================

• memory leak!

• t1 = &((*t1)->next);
*t1 = (*t1)->next;
can u explain me what each of these two lines are doing?

• I want to know why does `slow->next = slow->next->next;` work even we have already `delete slow->next;`?

• Hi, tim14. Your code is good. But it seems that you need a good format setting to help people read it.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.