1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00
LeetCode/java/linked-list-cycle.java
Matej Focko 2d4ba86cbe
java: add «141. Linked List Cycle»
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-03-06 23:05:20 +01:00

21 lines
402 B
Java

/**
* Definition for singly-linked list. class ListNode { int val; ListNode next; ListNode(int x) { val
* = x; next = null; } }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
var x = head;
var y = head;
while (y != null && y.next != null) {
x = x.next;
y = y.next.next;
if (x == y) {
return true;
}
}
return false;
}
}