Linked List Cycle II
来源:互联网 发布:淘宝企业店铺委托书 编辑:程序博客网 时间:2024/05/18 23:54
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?
Subscribe to see which companies asked this question
/** * 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) { if(head==NULL) return head; ListNode* slow=head; ListNode* fast=head; while(fast!=NULL && fast->next!=NULL) { slow=slow->next; fast=fast->next->next; if(slow==fast) break; } if(fast==NULL || fast->next==NULL) return NULL; slow=head; while(slow!=fast) { slow=slow->next; fast=fast->next; } return fast; }};
0 0
- 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
- 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
- 过拟合与正则化
- 类和对象
- Java解决java.io.FileNotFoundException: E:\work\work (拒绝访问。)
- 基于logistic regression的严格矩阵求导
- 两种方法实现卫星式菜单
- Linked List Cycle II
- NOIPの模拟_2016_7_19_t2_弄提纲
- Hadoop环境搭建(一)
- web中拖拽函数的封装
- GitHub for windows使用教程(三)
- Idea Maven 报错 Cannot reconnect
- python3的错误和异常操作
- 7/19android培训第九天
- 安全问题