반응형

https://leetcode.com/problems/linked-list-cycle/description

 

Linked List Cycle - LeetCode

Can you solve this real interview question? Linked List Cycle - Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuo

leetcode.com

 

투포인터를 이용하면 O(1)의 메모리로 풀 수 있었습니다.

2칸씩 이동하는 포인터가 1칸씩 이동하는 포인터가 만날 수 있다면, 사이클이 있는 것입니다.

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(!head) return false;
        ListNode *slow = head, *fast = head;
        while(fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast) return true;
        }
        return false;
    }
};
반응형

+ Recent posts