cycledetection 3

[LinkedLists: Fast and Slow Pointers] 287. Find the Duplicate Number ★★★★★

287. Find the Duplicate Number 이 문제 LeetCode 287. Find the Duplicate Number는 배열을 수정하지 않고,**O(1)**의 추가 공간만으로 중복된 숫자 하나를 찾아야 하는 문제예요. 내 코드class Solution: def findDuplicate(self, nums: List[int]) -> int: # nums 수정하지 말고, 공간복잡도는 O(1) 로..!! # n+1 개수 n = len(nums)-1 res = 1 for i in range(2, n+1): res ^= i for n in nums: res ^= ..

LeetCode/Grind169 2025.04.06

[LinkedLists: Fast and Slow Pointers] Linked List Cycle II ★★★★★

142. Linked List Cycle II 이 문제는 Linked List Cycle Detection 문제 중 하나로,**Floyd's Cycle Detection Algorithm (Tortoise and Hare)**를 활용하여 해결할 수 있습니다.아래는 단계별 풀이 방법입니다.1. Floyd’s Cycle Detection Algorithm (토끼와 거북이)두 포인터(Tortoise와 Hare)를 사용하여 Linked List를 순회합니다.속도 차이를 이용해 두 포인터가 같은 노드에 도달하면 사이클이 있음을 확인할 수 있습니다.사이클이 발견되면, 사이클의 시작 노드를 찾는 추가 작업을 수행합니다.2. 단계별 풀이1단계: 사이클 존재 여부 확인slow 포인터는 한 번에 한 노드씩 이동합니다.fa..

LeetCode/NeetCode 2025.01.27

[LinkedLists: Fast and Slow Pointers] List(싸이클탐지): 141. Linked List Cycle

141. Linked List Cycle https://youtu.be/gBTe7lFR3vc?si=VAfWxrUDCliFGt2Q # Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: # 연결 되어 있으면, fast 루프랑 slow 루프는 만날것이다 라는 아이디어 slow, fast = head, head while fast and fast.next: ..