Java for LeetCode 142 Linked List Cycle II
2015-06-04 20:34
686 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, return
Follow up:
Can you solve it without using extra space?
解题思路,本题和上题十分类似,但是需要观察出一个规律,参考LeetCode:Linked List Cycle II
JAVA实现如下:
null.
Follow up:
Can you solve it without using extra space?
解题思路,本题和上题十分类似,但是需要观察出一个规律,参考LeetCode:Linked List Cycle II
JAVA实现如下:
public ListNode detectCycle(ListNode head) { ListNode fast = head, slow = head; boolean isLoop=false; while (fast != null) { slow = slow.next; ListNode temp = fast.next; if (temp == null) return null; fast = temp.next; if (fast == slow){ isLoop=true; break; } } while (isLoop) { if (slow == head) return slow; slow = slow.next; head = head.next; } return null; }
相关文章推荐
- java-IO流(中)
- Java语言描述:回溯法之01背包问题(最易理解的回溯法讲解)
- Java 内存区域和GC机制
- Android项目中使用Eclipse导出jar文件
- Java的GC机制及算法
- Ubuntu下eclipse部署mysql jdbc驱动
- Java实验三
- 严重Exception starting filter struts2的解决
- java第三次试验报告
- myeclipse连接mysql遇到的小问题
- Java数据库之JDBC
- Eclipse安装SVN插件(转载)
- JAVA RMI 原理与实现
- struts2文件异步上传带进度条(二)
- Java文本编辑器中遇到的问题详解
- 我的第一次Spring学习
- java中serializable解析及用途
- 20135208 JAVA第三次实验
- [转载]JAVA中this用法小结
- spring_aop