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.

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
ListNode* slow2 = head;
while(slow2 != slow){
slow = slow->next;
slow2 = slow2->next;
}
return slow;
}
}
return nullptr;
}
};

Linked List Cycle II的更多相关文章

  1. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  2. 15. Linked List Cycle && Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...

  3. Java for LeetCode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. 【LeetCode练习题】Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  6. [Linked List]Linked List Cycle,Linked List Cycle II

    一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...

  7. Linked List Cycle && Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...

  8. LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

    1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...

  9. 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)

    题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...

  10. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

随机推荐

  1. qml package 的使用

    什么时候使用这个.就是多个view使用同一个deleagte的时候. The Package class is used in conjunction with VisualDataModel to ...

  2. java.lang.ThreadGroup.enumerate

    java.lang.ThreadGroup.enumerate(Thread[] list) 方法复制该线程组及其子组中的所有活动线程到指定的数组. 声明 以下是java.lang.ThreadGro ...

  3. 一个快速查看API的汇编和机器码的工具.发布源码

    提供一个早年写的一个小工具,一直在用,赶紧很顺手,特推荐给大家. 欢迎垂询. 1,在OD正在跟踪分析某个保护壳的一段code的时候,感觉似曾相识,好像在哪里见过,好像是某个API.----这个时候你就 ...

  4. HDU 1754 单点更新,求区间最大值

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. jQuery对表单元素的取值和赋值操作代码

    使用常规的思路:$(“#keyword”).value 取值是取不到的,因为此时$(‘#keydord’)已经不是个element,而是个jquery对象,所以应该使用:$(“#keyword”).v ...

  6. 使用rgba色实现背景色透明

    父元素css属性:background-color: #000;  background: rgba(0,0,0,.5); //现代浏览器属性,使用rgba色实现透明,对子属性不继承  filter: ...

  7. vim的编码设置

    VIM的相关字符编码主要有三个参数 fencs: 它是一个编码格式的猜测列表.当用vim打开某个文件时,会依次取这里面的编码进行解码,如果某个编码格式从头至尾解码正确,那么就用那个编码 fenc:它是 ...

  8. CCNA 6.5

    no sh (no shutdown : start the interface)   router rspf 1 network x.x.x.x x.x.x.x area 0 int (interf ...

  9. Apache Jmeter(1)

    Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域. 它可以用于测试静态和动态资源例如静态文件. ...

  10. [Windows驱动]流媒体驱动开发

    从Windows98开始,Windows流媒体驱动遵循Windows Driver Model(WDM)模型并使用Kernel Streaming(KS)组件.Kernel Streaming(KS) ...