Skip to content

Commit

Permalink
Create HasCycle.java
Browse files Browse the repository at this point in the history
  • Loading branch information
codingeekks authored Oct 5, 2022
1 parent 2798384 commit b7c5f2f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions data_structures/Linked_list/java/HasCycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ListNode {
int val;
ListNode next;

public ListNode() {
}

ListNode(int x) {
val = x;
next = null;
}
}
public class HasCycle{
public boolean HsCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;

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

0 comments on commit b7c5f2f

Please sign in to comment.