/*
* 寻找链表中环的入口节点.cpp
*
* Created on: 2018年4月10日
* Author: soyo
*/
#include<iostream>
using namespace std;
struct Node{
int num;
Node * next;
};
Node * creat()
{
Node *head;
Node *p;
head=new Node;
p=head;
p->num=;
p->next=NULL;
return head;
}
Node * insert(Node*head,int data)
{
Node *p1,*p;
p1=new Node;
p1->num=data;
p1->next=NULL;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=p1;
return head;
}
Node * makeListCircle(Node *head,int n) //n代表链表第几个节点处设置为环的入口节点
{
Node *p=head;
Node *p2; //环的入口节点
int count=;
while(p->next!=NULL)
{
p=p->next;
count++;
if(count==n)
{
p2=p;
}
}
p->next=p2;
return head;
} void printl(Node *head)
{
Node *p=head;
while(p!=NULL)
{
cout<<"数据为:"<<p->num;
p=p->next;
}
cout<<endl;
}
void printl_circle(Node *head)
{
Node *p=head;
int count=;
while(p!=NULL)
{
if(count==) break; //控制打印的总个数(不然无限循环)
cout<<"数据为:"<<p->num;
p=p->next;
count++;
}
cout<<endl;
}
Node* meetNode(Node*head) //找到环中的节点
{
if(head==NULL)
return NULL;
Node *pslow;
pslow=head->next;
Node *pfast;
pfast=pslow->next;
while(pfast!=NULL&&pslow!=NULL)
{
if(pfast==pslow)
return pfast;
pfast=pfast->next;
pslow=pslow->next;
if(pfast!=NULL)
pfast=pfast->next;
}
return NULL;
}
Node * ringEntrance(Node * head) //找到环的入口
{
Node*meetN=meetNode(head);
int count=;
Node *p=meetN;
Node *p2;
while(p->next!=meetN)//确定环中节点的数目
{
p=p->next;
count++;
}
p=head;
for(int i=;i<count;i++)
{
p=p->next;
}
p2=head;
while(p!=p2)
{
p=p->next;
p2=p2->next;
}
return p2;
} int main()
{
Node *head=creat();
// cout<<head->num<<endl;
int i,data;
for(i=;i<;i++)
{
cin>>data;
head=insert(head,data);
}
printl(head);
makeListCircle(head,);
printl_circle(head);
Node *p;
p=ringEntrance(head); //环的入口节点
cout<<"环的入口节点的值为:"<<p->num<<endl;
}

结果:

数据为:10数据为:1数据为:2数据为:3数据为:4数据为:
数据为:10数据为:1数据为:2数据为:3数据为:4数据为:5数据为:4数据为:5数据为:4数据为:
环的入口节点的值为:

C++实现查找链表中环的入口节点的更多相关文章

  1. 剑指Offer:链表中环的入口节点【23】

    剑指Offer:链表中环的入口节点[23] 题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 第一步确定链表中是否包含环,怎么确定呢?我们定义两个指针橙和 ...

  2. php实现找链表中环的入口节点(画图、看评论)

    php实现找链表中环的入口节点(画图.看评论) 一.总结 画图.看评论 二.php实现找链表中环的入口节点 题目描述: 一个链表中包含环,请找出该链表的环的入口结点. 三.代码 第一步,找环中相汇点. ...

  3. 【剑指offer】面试题 23. 链表中环的入口节点

    面试题 23. 链表中环的入口节点

  4. 剑指Offer(书):链表中环的入口节点

    题目:给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public ListNode EntryNodeOfLoop(ListNode pHead) { //第一步,查找是 ...

  5. 剑指offer(55)链表中环的入口节点

    题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 1.一快一慢指针,先找到碰撞点. 2.然后碰撞点到入口节点的距离就是头结点到入口节点的距离. 具体原理可 ...

  6. python剑指offer 链表中环的入口节点

    题目: 一个链表中包含环,请找出该链表的环的入口结点. 思路: 先说个定理:两个指针一个fast.一个slow同时从一个链表的头部出发, fast一次走2步,slow一次走一步,如果该链表有环,两个指 ...

  7. 剑指offer——25链表中环的入口节点

    题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null.   题解: 使用快慢指针即可,若快慢指针会相遇,则有环,否则快指针先到空节点: 此时,快指针从此处一次移一步遍历, ...

  8. 剑指offer——面试题23:链表中环的入口节点

    函数: ListNode* MeetingNode(ListNode* pHead) { if(pHead==nullptr) return nullptr; ListNode* quickNode= ...

  9. [剑指Offer]23-链表中环的入口节点

    题目链接 https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&t ...

随机推荐

  1. golang几种post请求方式

    get请求 get请求可以直接http.Get方法,非常简单. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 func httpGet() {     resp, err := h ...

  2. Android新手入门2016(7)--布局

    布局,这个在服务端变成是没有的,也是服务端的人学习client的一道坎吧. 曾经用cocos2d-x写小游戏的时候就是这个非常难懂,或者能用,可是理解不多的话,非常难写出好的布局,难以适合商业化的应用 ...

  3. 重装系统(Win)

    有朋友问我,重装系统该怎样操作呢? 1. 硬盘重装 官网:http://www.heiyunwang.com/ ,点击下载软件:http://dlsw.baidu.com/sw-search-sp/s ...

  4. weex 项目开发(三) weexpack + weex-ui

    github地址:weex-ui https://github.com/alibaba/weex-ui 官网: https://alibaba.github.io/weex-ui/#/cn/ 创建项目 ...

  5. freeIPMI README && issue about OpenIPMI kernel driver

    http://www.gnu.org/software/freeipmi/README FreeIPMI - Copyright (C) 2003-2013 FreeIPMI Core Team Fr ...

  6. VLFeat中SIFT特征点检测

    本代码使用VLFeat库中的函数对一幅图像进行了SIFT检测 需要事先配置好VLFeat和OpenCV,VLFeat的配置参考前一篇博文,OpenCV的配置网上一大堆,自己去百度 #include & ...

  7. Xcode6.1 Prefix.pch添加方式

     本文转载:http://blog.csdn.net/foolsong/article/details/40653497     在Xcode6.1中创建工程默认是没有Prefix.pch文件的,需要 ...

  8. gradle配置远程仓库(以及使用本地maven仓库)

    allprojects{ repositories { mavenLocal() def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content ...

  9. 使用 fetch 代替 ajax(在不支持的浏览器上使用 XHR); This kind of functionality was previously achieved using XMLHttpRequest.

    原生 JS Ajax,GET和POST 请求实例代码_javascript技巧_脚本之家 https://www.jb51.net/article/86157.htm 更新时间:2016年06月08日 ...

  10. ln mv 发挥一个物体的元作用

    # tar xf ~/tools/mongodb-linux-x86_64-3.4.0.tgz -C /usr/local/; ln -sf /usr/local/mongodb-linux-x86_ ...