-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0141-LinkedListCycle.cs
37 lines (34 loc) · 1007 Bytes
/
0141-LinkedListCycle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//-----------------------------------------------------------------------------
// Runtime: 96ms
// Memory Usage: 25.6 MB
// Link: https://leetcode.com/submissions/detail/352953685/
//-----------------------------------------------------------------------------
namespace LeetCode
{
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class _0141_LinkedListCycle
{
public bool HasCycle(ListNode head)
{
if (head == null || head.next == null) return false;
ListNode slow = head, fast = head.next;
while (slow != fast)
{
if (fast == null || fast.next == null) return false;
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}
}