Skip to content

Commit

Permalink
Created 141-Linked-List-Cycle.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
qwmhq committed Aug 11, 2022
1 parent b23c604 commit 8080f6d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions csharp/141-Linked-List-Cycle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/

public class Solution {
public bool HasCycle(ListNode head) {
ListNode slow = head, fast = head;

while (fast != null && fast.next != null)
{
fast = fast.next.next;
slow = slow.next;
if (slow.Equals(fast)) return true;
}
return false;
}
}

0 comments on commit 8080f6d

Please sign in to comment.