LeetCode_linked-list-cycle-ii
来源:互联网 发布:淘宝 安能小包发货 编辑:程序博客网 时间:2024/06/10 05:28
public ListNode detectCycle(ListNode head) {
// 1.判断有没有环
ListNode quick = head;
ListNode slow = head;
int num=0;
if (head == null || head.next == null || head.next.next == null) {
return null;
}
while ( quick.next != null&&quick.next.next!=null){
if(quick==slow){
num++;
}
if(num==2){
break;
}
quick = quick.next.next;
slow = slow.next;
}
if (quick.next==null||quick.next.next == null) {
return null;
}
// 2.慢点继续一步步走,快点从头一步步走,相遇点即为所求
quick = head;
while (quick != slow) {
quick = quick.next;
slow = slow.next;
}
return quick;
}
// 1.判断有没有环
ListNode quick = head;
ListNode slow = head;
int num=0;
if (head == null || head.next == null || head.next.next == null) {
return null;
}
while ( quick.next != null&&quick.next.next!=null){
if(quick==slow){
num++;
}
if(num==2){
break;
}
quick = quick.next.next;
slow = slow.next;
}
if (quick.next==null||quick.next.next == null) {
return null;
}
// 2.慢点继续一步步走,快点从头一步步走,相遇点即为所求
quick = head;
while (quick != slow) {
quick = quick.next;
slow = slow.next;
}
return quick;
}
阅读全文
0 0
- Leetcode_linked-list-cycle-ii
- LeetCode_linked-list-cycle-ii
- LeetCode_Linked List_Linked List Cycle II
- leetcode_Linked List Cycle
- LeetCode_Linked List Cycle
- LeetCode_Linked List_Linked List Cycle
- Leetcode_linked-list-cycle(c++ and python version)
- LeetCode_Linked List_Remove Duplicates from Sorted List II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- bot framework2——与用户交互
- 如何在安卓中使用线程池(ThreadPoolExecutor)
- java用递归删除目录
- git查看远程仓库信息
- Playgrounds配件Parrot Education的深入发掘(一)
- LeetCode_linked-list-cycle-ii
- mysql语法
- 二分搜索——在有序数组中找到目标函数出来的最左端的位置
- 学习记录:接口功能配置之 Settings及QuickSettings
- POJ 1269 Intersecting Lines(直线相交判断,求交点)
- 111. Minimum Depth of Binary Tree
- Dubbo源码阅读之 客户端远程调用
- webpack
- 多数据速率变换中的插值和抽取