Implement an algorithm to find the nth to last element of a singly linked list.
分析:查找鏈表倒數第n個元素,這在劍指offer上有相同的習題
Node* get_lastn(Node* head,int n){
if(head==NULL||n<=0) return NULL;
Node* front=head;
Node* behind=head;
while(n>1){
behind=behind->next;
if(behind==NULL) return NULL;
n--;
}
while(behind->next!=NULL){
front=front->next;
behind=behind->next;
}
return front;
}