leetcode01 --- 环形链表判定
题目
. - 力扣(LeetCode) 环形链表判定
代码
/**
* 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) {
ListNode node = head;
ListNode preNode = head;
while (node.next != null) {
boolean cycle = false;
while (preNode != node) {
// System.out.println("val = " + preNode.val);
if (node.next == preNode) {
return true;
}
preNode = preNode.next;
}
// System.out.println("cycle = " + cycle);
//if (cycle) {
// return true;
//}
node = node.next;
preNode = head;
}
return false;
}
}