ListNode* FindKthToTail(ListNode* pHead, int k) {
// write code here
if(!k || !pHead){//链表为空
return NULL;
}
ListNode *p=pHead,*q=pHead;
int n=1;//记录链表长度
while(p->next){
p=p->next;
n++;
if(n>k){
q=q->next;
}
}
if(n<k){//超过链表长度
return NULL;
}
return q;
}

京公网安备 11010502036488号