关于链表中是否带环并且找到环的入口点
面试题一:判断链表是否带环
intFndLoop(pLinkListlist){pLinkNodefast=list->pHead;pLinkNodeslow=list->pHead;assert(list);while(fast!=NULL&&fast->next!=NULL){slow=slow->next;fast=fast->next->next;if(fast!=NULL&&slow==fast){return1;//有环}}return0;//无环}
面试题二:找到环的入口点
pLinkNodeFndLoopNode(pLinkListlist){pLinkNodefast=list->pHead;pLinkNodeslow=list->pHead;assert(list);while(fast!=NULL&&fast->next!=NULL){slow=slow->next;fast=fast->next->next;if(fast!=NULL&&slow==fast){break;}}slow=list->pHead;while(slow!=fast){slow=slow->next;fast=fast->next;}returnslow;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。