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 ...
随机推荐
- Liunx常用命令(备用)
常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir ...
- sublime 高速打开跳转至关联文件
在下一枚web前端,近期在用sublime text2编辑器写前端.因为页面较多,项目较大,所以难免出现非常多引用文件和一些js的teample模板. 问题:在Sublime Text编写代码过程中要 ...
- quilt - 制作patch的工具
quilt - 制作patch的工具 在尝试为openwrt做一个patch时,查到这个工具.openwrt官方已经有很详细的文档对步骤进行说明了. quilt并不是专为openwrt的开发工具.qu ...
- 编程算法 - 数组中出现次数超过一半的数字 代码(C)
数组中出现次数超过一半的数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 数组中有一个数字出现的次数超过数组长度的一半, 请找出这个数字. ...
- HDU 6058 Kanade's sum 二分,链表
Kanade's sum Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th larg ...
- webservice client setTimeOut
一:eclipse生成的client,基于axis client_sub.getOptions().setTimeOutInMilliSeconds(1000*60); client_sub表示一个客 ...
- php网站前台utf-8格式有时会出现莫名其妙的空白行,重新保存下编码格式就可以了
php网站前台utf-8格式有时会出现莫名其妙的空白行,重新保存下编码格式就可以了.
- oracle中去掉文本中的换行符、回车符、制表符
一.特殊符号ascii定义 制表符 chr(9) 换行符 chr(10) 回车符 chr(13) UPDATE tc_car_order set USE_REASON = REPLACE('USE ...
- About "self"
Class method can't refer derectly to instance variables. Within the body of a class method, self re ...
- redis09---redis 服务器端命令
redis 服务器端命令 db0,db1,db2是数据库,外层是服务器,服务器下面有20个数据库. :>time ) "" //多少秒 ) "" //多少 ...