leetcode-hot链表专题——141. 环形链表

141. 环形链表

leetcode-hot链表专题——141. 环形链表_第1张图片

  • fast一次走两步,slow一次走一步
  • 若有环,fast、slow一定会在环内相遇
bool hasCycle(ListNode *h) {
        ListNode *fast = h,*slow = h;
        while(fast != NULL && fast->next != NULL){
            slow = slow->next;
            fast = fast->next->next;
            if(fast == slow) return true;
        }
        return false;
    }

你可能感兴趣的:(leetcode题目,链表,leetcode,数据结构)