# Remove Nth Node From End of List

• This post is deleted!

• What kind of error message is threw?

• @twittidai Thank you.But I think I have sloved the problem and it's because another method's error.

• This post is deleted!

• The second solution is not "one pass", its actually a simultaneous double pass..
So actually two passes.

• @romansky I think when we mean one-pass, it means that the list is completely traversed.Since in the second solution, since the list is traversed in complete only once, it is referred as a one-pass solution.

• @ironman7 I find that the number of times I traverse n members of a list the more acceptable meaning to "one-pass", as is mentioned in the body "one traversal", but is infact two pointers advancing in some-kind of tandem.
I could (somewhat) understand if you were to invoke multiple functions for the same member while traversing (and by this possibly save some pointer lookups) but this is not the case.

• Will given solution hit real-time error when calling removeNthFromEnd(NULL, 0) ??

• As other people said, the second method is 'simultaneous two pass'. For exactly one pass, I can use a (n + 1)length buffer which stores the pointer.

class Solution(object):
"""
:type n: int
:rtype: ListNode
"""
buffer = (n + 1) * [0]
i = 0
i += 1
i = i%(n + 1)
if buffer[i] == 0 :
return buffer[0].next
else:
buffer[i].next = buffer[i].next.next

• ``````232
``````

`22`
`1`

• As other people said, the second method is 'simultaneous two pass'. For exactly one pass, I can use a (n + 1)length buffer which stores the pointer.
Here is my code:

``````class Solution(object):
"""
:type n: int
:rtype: ListNode
"""
buffer = (n + 1) * [0]
i = 0
i += 1
i = i%(n + 1)
if buffer[i] == 0 :
return buffer[0].next
else:
buffer[i].next = buffer[i].next.next
``````

• don't even understand part of the explanation for the 2nd approach. What is D? and given n = 2, n+ 1 would be 3 which means the pointer should point at 4.

• Hi, D is dummy head (ListNode dummy in the program). Given n=2, n+1 would be 3, which means the pointer of first would move to next 3 times, starting from dummy head. It will point to 3 when the loop of "for" is finished

# The code above would run into NullPointerException if n > length of the list (including dummy head), it can be updated to:

for (int i = 1; i <= n+1; i++) { // do "move to next" from dummyHead (n + 1) times
fast = fast.next;
if (fast == null && i != n+1) { // fast == null && i == n+1 means you would like to delete the last node
}
}

• With `n > L / 2` second solution will not work

• I was wondering what should be the output if list just has 1 node and n = 1. In that case isn't the head node also 1st node of the list? LeetCode somehow is claiming the answer as wrong and expects nullptr as the answer. Can someone explain?

• For the corner case in the one pass, here's a solution where you do not need to store a buffer:

``````ListNode* removeNthFromEnd(ListNode* head, int n) {
// If it's a single element, return NULL.
if (head->next == NULL) return NULL;
while(p2->next != NULL && n--) p2 = p2->next;
// After the above while loop, we might still have some n left
// if the p2 reaches end quickly. We need to consider the case
// when n > length of list.
while(p2->next != NULL) {
p1 = p1->next;
p2 = p2->next;
}
// This is the case when n is left
if (n > 0) {
*p1 = *(p1->next);
} else {
p1->next = p1->next->next;
}
}
``````

This is O(n) without extra space.

• public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (result.next == null)
return null;
int i = 0;
while(tem.next != null){
if(i>n-1) result = result.next;
else i++;
tem = tem.next;
}
result.next = result.next.next;

``````}
``````

}

• /**

• struct ListNode {
• ``````int val;
``````
• ``````ListNode *next;
``````
• ``````ListNode(int x) : val(x), next(NULL) {}
``````
• };
/
class Solution {
public:
ListNode
int dis = 0;
while(right)
{
if(dis == n)
{ left = mid; mid = mid->next ;}
else dis++;
right = right->next;
}