C++实现查找链表中环的入口节点
/*
* 寻找链表中环的入口节点.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++实现查找链表中环的入口节点的更多相关文章
- 剑指Offer:链表中环的入口节点【23】
剑指Offer:链表中环的入口节点[23] 题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 第一步确定链表中是否包含环,怎么确定呢?我们定义两个指针橙和 ...
- php实现找链表中环的入口节点(画图、看评论)
php实现找链表中环的入口节点(画图.看评论) 一.总结 画图.看评论 二.php实现找链表中环的入口节点 题目描述: 一个链表中包含环,请找出该链表的环的入口结点. 三.代码 第一步,找环中相汇点. ...
- 【剑指offer】面试题 23. 链表中环的入口节点
面试题 23. 链表中环的入口节点
- 剑指Offer(书):链表中环的入口节点
题目:给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public ListNode EntryNodeOfLoop(ListNode pHead) { //第一步,查找是 ...
- 剑指offer(55)链表中环的入口节点
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 1.一快一慢指针,先找到碰撞点. 2.然后碰撞点到入口节点的距离就是头结点到入口节点的距离. 具体原理可 ...
- python剑指offer 链表中环的入口节点
题目: 一个链表中包含环,请找出该链表的环的入口结点. 思路: 先说个定理:两个指针一个fast.一个slow同时从一个链表的头部出发, fast一次走2步,slow一次走一步,如果该链表有环,两个指 ...
- 剑指offer——25链表中环的入口节点
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题解: 使用快慢指针即可,若快慢指针会相遇,则有环,否则快指针先到空节点: 此时,快指针从此处一次移一步遍历, ...
- 剑指offer——面试题23:链表中环的入口节点
函数: ListNode* MeetingNode(ListNode* pHead) { if(pHead==nullptr) return nullptr; ListNode* quickNode= ...
- [剑指Offer]23-链表中环的入口节点
题目链接 https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&t ...
随机推荐
- htmlcxx取指定字段实例
#include <string> #include <iostream> #include <sstream> #include <htmlcxx/html ...
- ARM汇编指令MCR/MRC学习
MCR指令将ARM处理器的寄存器中的数据传送到协处理器的寄存器中.假设协处理器不能成功地运行该操作.将产生没有定义的指令异常中断. 指令的语法格式: MCR{<cond>} p15, 0, ...
- weexpack build android 和 weexpack run android 报错 及 解决方案
1. weexpack build android (1)Configuring > 0/3 projects > root project > Resolving dependen ...
- IE8.0登录Oracle EBS后报Oracle error 1403错
IE8.0登录Oracle EBS后报错,登录页面打开没有问题.只是输入username和password然后登录,遇到下面错误: <PRE>Oracle error 1403: java ...
- 龙书D3D11章节习题答案(第四章)
下面答案仅供參考,有错欢迎留言. Chapter 4:Direct3D Initialzation 1. Modify the previous exercise solution by dis ...
- TinyXML:属性
TiXmlAttribute: 代表XML中的属性,TiXmlAttribute中定义了一系列对属性的操作 TiXmlAttribute的友元类: friend class TiXmlAttribut ...
- 为activity添加左右手势识别
android开发中为activity添加左右手势识别.如右滑关闭当前页面 /* * for左右手势 * 1.复制以下的内容到目标Activity * 2.目标Activity的onCreate()调 ...
- iOS UIButton选中状态切换
UIButton*payBtn = [UIButtonbuttonWithType:UIButtonTypeCustom]; payBtn.frame=CGRectMake(size.width-24 ...
- 基于ADB框架Robotium跨进程操作
转自:http://blog.csdn.net/qingchunjun/article/details/42580937 2015年2月3日更新: 有些朋友在用真机尝试本方法时,抛出了InputStr ...
- http权威指南(一)-Http概述
Http概述 在Web中,不管是浏览器还是server都是通过Http相互通信的.那么Http是怎样工作的呢? 首先,client向server发送Http请求,server会在Http响应中回送所请 ...