判断链表中是否有环存在
2015-07-10 17:34
323 查看
思路:
用两个指针,pSlow,pFast,就是一个慢一个快
慢的一次跳一步,
快的一次跳两步,
什么时候快的追上慢的了就表示有环(pSlow == pFast )。
实现如下:
用两个指针,pSlow,pFast,就是一个慢一个快
慢的一次跳一步,
快的一次跳两步,
什么时候快的追上慢的了就表示有环(pSlow == pFast )。
实现如下:
struct listtype { int data; struct listtype * next; }list; int find_cicle(list *head) { list *pFast=head; list *pSlow=head; if (pFast==NULL) { return -1; } while(pFast && pFast->next) { pFast=pFast->next->next; pSlow=pSlow->next; if (pFast==pSlow) { return 1; } } return 0 }
相关文章推荐
- PyPI中国镜像 2015
- NSUserDefaults的读写
- 对各种语言的偏见
- 创建Imgbutton 按钮
- Swift入坑--单例
- hiho一下 连通性二·边的双连通分量
- MYSQL区分大小写
- java.lang.UnsupportedOperationException异常
- Android开发—Navigation Drawer(侧边栏菜单实现)
- 2015.7.10(全部涨停!想逢高出货,但是担心周一创新高)
- 手动挡停车技巧;换挡注意
- Search Insert Position
- Cocos2dx 3.6 CCLable enableShadow error.
- 【VMware】VMware Workstation中虚拟机网络配置
- Linux~centos安装LAMP环境
- WWDC15 Session笔记 - iOS 9 多任务分屏要点
- Java中类的继承与接口
- JQuery EasyUI学习框架
- 打箱子&地形&鼠标点击移动总结
- Shared Everything和share-nothing区别