要点:标记已经走过的节点,如果再次遇到已经走过的节点,那就有环,否则无环。

/**
 * 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) {
        boolean flag = false;
        while(null != head){
            if(head.val != 100001){
                head.val = 100001;
                head = head.next;
            }else{
                flag = true;
                break;
            }
        }
        return flag;
    }
}