leetcode--Linked List Cycle II
来源:互联网 发布:java 引号转义 编辑:程序博客网 时间:2024/06/05 06:00
做这道题的时候离做Linked List Cycle I 已经有一段时间了,只是隐约记得用到快慢指针,就自己写了一段代码,也是accepted,但是效率较低,如下:
方法一:
ListNode *detectCycle(ListNode *head) { ListNode *p=head,*q=head,*r=head; while(q!=NULL&&q->next!=NULL) { q=q->next; if(q!=p) //p和q相等判断是否有环 { q=q->next; p=p->next; } else //有环存在 { while(q->next!=r) { q=q->next; if(q==p) { r=r->next; } } return r; } } return NULL; }方法二:
搜题解的时候仔细研究了自己的快慢指针和别人的快慢指针,我自己写的方法一中的两个指针并不是严格的慢指针走一步时快指针走两步,所以判断出的两个指针的相遇链表结点在一些有环单链表中是不同的,那么在进行查找环开始结点的时候方法也就不同了。在下面的方法中使用了严格的快慢指针,主要思想参考了http://home.cnblogs.com/u/648099/
http://blog.sina.com.cn/s/blog_6f611c300101fs1l.html
他们用清晰的图示和简单数学推导已经讲述得很清晰了,赞!
下面是我自己的代码:
ListNode *detectCycle(ListNode *head) { ListNode *fast=head,*slow=head; while(fast!=NULL&&fast->next!=NULL){ slow=slow->next; fast=fast->next->next; if(fast==slow) break; } if(fast==NULL||fast->next==NULL) return NULL; slow=head; while(slow!=fast){ slow=slow->next; fast=fast->next; } return fast; }
0 0
- 【LeetCode】 Linked List Cycle II
- Leetcode: Linked List Cycle II
- <Leetcode>Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode:Linked List Cycle II
- Leetcode Linked List Cycle II
- [LeetCode]Linked List Cycle II
- leetcode Linked List Cycle II
- LeetCode | Linked List Cycle II
- Leetcode: Linked List Cycle II
- [LeetCode] - Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode - Linked List Cycle II
- leetcode Linked List Cycle II
- Linked List Cycle II -- LeetCode
- LeetCode Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Android使用mvp模式入门
- FloatingActionButton的使用
- Android NDK环境配置
- 链接字符串各属性介绍
- Apache Felix Event Admin在ServiceMix容器下使用
- leetcode--Linked List Cycle II
- 构建微服务-第一章-什么是微服务_008总结
- ZOJ3229:Shoot the Bullet 有源汇上下界最大流
- Log4j 配置详解
- three.js制作一个立体几何体
- Windows10下搭建React Native Android开发环境
- android inflater 用法
- Snackbar的使用
- 仿微信引导界面PageAdapter