/*

    • @lc app=leetcode.cn id=141 lang=javascript
    • [141] 环形链表
      */

    // @lc code=start
    /**

    • Definition for singly-linked list.
    • function ListNode(val) {
      1. this.val = val;
    • this.next = null;
      
    • }
      */

    /**

    • @param {ListNode} head

    • @return {boolean}
      */
      // var hasCycle = function(head) {
      // while(head){
      // if(head.flag){
      // return true;
      // }else{
      // head.flag = true;
      // head = head.next;
      // }
      // }
      // return false;
      // };
      var hasCycle = function(head) {
      if (head == null || head.next == null) {
      return false;
      }
      let first = head.next
      let last = head;
      while(first){
      last = last.next;
      first = first?.next?.next;
      if(first === last){
      return true;
      }
      }
      return false;
      };
      // @lc code=end