-
Notifications
You must be signed in to change notification settings - Fork 56
/
45.CycleHeadLinkedList.cs
78 lines (68 loc) · 1.87 KB
/
45.CycleHeadLinkedList.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//Given a singly Linked List
//If there is a cycle in the Linked List, return the head of the cycle
//If there is no cycle in the Linked List, return null
using System;
using System.Collections;
namespace FindLoopLinkedList
{
class Node //simple Node class
{
public int value{ get; private set;}
public Node next{ get; set;}
public Node(int value)
{
this.value = value;
this.next = null;
}
}
class Finder
{
//classic linked list approach: use two pointers, one slow, one fast!
//After finding the cycle, move slow pointer to the head of LinkedList
//Then move the two pointers one step further at each time,
//when they meet again, the meeting point is the head node of cycle
public static Node FindLoop(Node head)
{
Node slow = head;
Node fast = head;
while (slow != null && fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) //if two pointers meet, there is a loop
break;
}
//two cases: fast is now null or fast is at the last node of list
//so we need to check both fast and fast.next
//if either fast or fast.next is null, we know there is no cycle
//cannot check fast.next first, because if fast is now null, there
//gonna be nullpointer exception
if (fast == null || fast.next == null)
return null;
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
class MainClass
{
public static void Main (string[] args)
{
Node node1 = new Node (1);
Node node2 = new Node (2);
Node node3 = new Node (3);
Node node4 = new Node (4);
Node node5 = new Node (5);
Node node6 = new Node (6);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = node4;
Console.WriteLine (Finder.FindLoop (node1).value);
}
}
}