求环的入口结点
2015-10-02 19:47
393 查看
题目描述:
原理:一个链表中包含环,请找出该链表的环的入口结点。
http://blog.sina.com.cn/s/blog_6a0e04380101a9o2.html
原理:一个链表中包含环,请找出该链表的环的入口结点。
http://blog.sina.com.cn/s/blog_6a0e04380101a9o2.html
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* MeetingNode(ListNode* pHead) { //找到环的入口结点 if(pHead==NULL) return pHead; ListNode *s=pHead->next,*f=NULL; if(s!=NULL) f=s->next; while(s!=NULL && f!=NULL) { if(s==f) return f; s=s->next; f=f->next; if(f!=NULL) f=f->next; } return NULL; } ListNode* EntryNodeOfLoop(ListNode* pHead) { if(pHead==NULL) return pHead; ListNode* meetNode=MeetingNode(pHead); if(meetNode==NULL) return NULL; ListNode* p=pHead; while(p!=meetNode) { p=p->next; meetNode=meetNode->next; } return p; } };
相关文章推荐
- Aizu 2450 Do use segment tree (树链剖分)
- [codevs2170]悠闲的漫步
- 写在NOIP2015前
- Google C++ Style Guide的哲学
- pat1012The Best Rank (25)
- Mysql 5.6.22编译安装
- Google C++ Style Guide的哲学
- LightOJ 1138 Trailing Zeroes (III)
- hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场
- 11i - 12 Gather Schema Statistics fails with Ora-20001 errors after 11G database Upgrade (文档 ID 781813.1)
- Star Schema完全参考手册读书笔记一
- zen coding和emmet的关系,zen coding 改名为了emmet
- JDK,JRE,JVM区别与联系
- 软工文档总结
- Picasso分析02
- 03crawler01 爬取直播电视剧列表
- 你所厌恶的正是别人渴望的
- 浅谈javascript的五种基本数据类型
- SimpleCalculator
- java字符串与整数之间的互相转换