/*
* 寻找链表中环的入口节点.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. ORACLE 内部原理

    http://www.ohsdba.cn/index.php?m=Article&a=index&id=46 内部原理 2016-05-04• 如何使用BBED 2016-04-16• ...

  2. 学习日记之抽象工厂模式和Effective C++

    抽象工厂模式(Abstract Factory):提供一个创建一系列相关或者相互依赖对象的接口.而无需制定他们详细的类. (1),工厂方法模式是定义一个用于创建对象的接口.让子类决定实例化哪一个类. ...

  3. SSM框架笔记

    配置 Project结构 SpringMVC启用 Spring MVC配置 Spring自己主动扫描 getBean的方法 SpringMVC与Struts2的差别 Log4j 拦截器与过滤器 文件U ...

  4. 【转载】C#之C#、.NET Framework、CLR的关系

    C#..NET Framework.CLR的关系 很多人没有将C#..NET Framework(.NET框架).CLR(Common Language Runtime,公共语言运行库)这三者之间的关 ...

  5. Expression Tree 学习笔记(一)

    大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...

  6. Intel Naming Strategy--2

    http://en.wikipedia.org/wiki/Intel_Corporation#Naming_strategy Naming strategy[edit] In 2006, Intel ...

  7. Development of Intel chipsets interconnection

    http://en.wikipedia.org/wiki/Chipset Chipset From Wikipedia, the free encyclopedia     A chipset is ...

  8. python 字符串前缀

    普通字符串 一般字符串都是已unicode编码,且和C类似,可以使用\来转义,比如 a = "test\ntest" print(a) 输出 test test 前面加r 在字符串 ...

  9. FastDFS的配置、部署与API使用解读(4)FastDFS配置详解之Client配置(转)

    一种方式是通过调用ClientGlobal类的初始化方法对配置文件进行加载,另一种是通过调用API逐一设置配置参数.后一种方式对于使用Zookeeper等加载属性的方式很方便. 1. 加载配置文件: ...

  10. CPU维修技术

    中央处理单元(Central Process Unit)简称CPU,是电脑的核心部件,负责处理和运算电脑内部所有数据.因其在电脑中的地位相当重要,所以一旦发生故障就会造成很严重的后果. [技术66]开 ...