코테준비 - Linked List Cycle

정상화·2023년 2월 26일

LeetCode

목록 보기
137/222

Linked List Cycle

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if (head == nullptr) {
            return false;
        }
        if (head->val == -200000) {
            return true;
        }
        head->val = -200000;
        return hasCycle(head->next);
    }
};
profile
백엔드 희망

0개의 댓글