leetcode142. Linked List Cycle II
来源:互联网 发布:算法交易是量化 编辑:程序博客网 时间:2024/04/30 08:42
142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
解法
快慢指针判断是否有环,有环的话,将其中一个指针等于head,从头开始一步一步走,直到再次重合,即为所求的点。
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode detectCycle(ListNode head) { if (head == null || head.next == null) { return null; } ListNode slow = head; ListNode fast = head.next; while (slow != fast) { if (fast == null || fast.next == null) { return null; } slow = slow.next; fast = fast.next.next; } slow = head; while (slow != fast.next) { slow = slow.next; fast = fast.next; } return slow; }}
0 0
- LeetCode142:Linked List Cycle II
- LeetCode142 Linked List Cycle II
- leetcode142. Linked List Cycle II
- Leetcode142. Linked List Cycle II
- leetcode142. Linked List Cycle II
- leetcode142~Linked List Cycle II
- [leetcode142]Linked List Cycle II
- leetcode142.Linked List Cycle II
- LeetCode142. Linked List Cycle II
- LCP142 LeetCode142. Linked List Cycle II
- LeetCode142—Linked List Cycle II
- Leetcode142 - Linked List Cycle II(链表)
- c++实现LeetCode142. Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- 华为OJ训练之0016-170102-记负均正
- linux问题经验
- 设计模式之单例模式
- ConcurrentHashMap 和 HashTable 的区别
- 最小二乘法及其C++实现
- leetcode142. Linked List Cycle II
- ios中给view添加圆角并指定位置
- Java基本概念和基础语法思维导图
- Python3.XX学习笔记--------(一)
- Vue.js学习系列(二十八)-- 计算属性(二)
- 蓝以中老师《高等代数》第03章:行列式,笔记
- 递归的又一次深刻体会
- 华为OJ训练之0017-170103-查找组成一个偶数最接近的两个素数
- 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用