파비의 매일매일 공부기록

2023.09.03 Today's Challenge 본문

Problem Solving/LeetCode

2023.09.03 Today's Challenge

fabichoi 2023. 9. 3. 23:45

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

 

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

투 포인터나 해쉬 테이블로 푸는 문제.

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        visited_nodes = set()
        current_node = head
        while current_node:
            if current_node in visited_nodes:
                return True
            visited_nodes.add(current_node)
            current_node = current_node.next
        return False
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.09.05 Today's Challenge  (0) 2023.09.05
2023.09.04 Today's Challenge  (0) 2023.09.04
2023.09.02 Today's Challenge  (0) 2023.09.02
2023.09.01 Today's Challenge  (0) 2023.09.01
2023.08.31 Today's Challenge  (0) 2023.08.31
Comments