您的位置:首页 > 职场人生

剑指offer 面试题15 找到单链表倒数第k个结点

2014-12-23 11:36 218 查看
struct ListNode{
int data;
ListNode *next;
};
ListNode* KthToTail(ListNode *pHead,int k){
if(pHead==NULL||k<=0) return NULL;
ListNode *pAhead=pHead;
ListNode *pBehind=pHead;
for(int i=0;i<k-1;++i){
if(pAhead->next!=NULL){
pAhead=pAhead->next;
}
else return NULL;
}
while(pAhead->next){
pAhead=pAhead->next;
pBehind=pBehind->next;
}
return pBehind;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: