반응형 전체 글72 [DSA][Linked List] 06. Copy List with Random Pointer LeetCode 138 06. Copy List with Random PointerA linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.Construct a deep copy of the list.The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node.Both the next and random poi.. 2025. 7. 3. [DSA][Linked List] 05. Remove Nth Node From End of List LeetCode 19 05. Remove Nth Node From End of ListGiven the head of a linked list, remove the nth node from the end of the list and return its head. [질문하기]- 빈 연결리스트가 주어질 수도 있나요? NO- n은 항상 연결리스트의 길이보다 작나요? YES- n이 1일 경우 가장 마지막 노드를 제거하는 것으로 이해하면 될까요? YES [아이디어]- 연결 리스트를 한 번 순회하여 전체 길이를 구한 뒤, 앞에서 (length - n)번째 노드를 찾아 삭제한다. - Two Pointer를 사용하여 포인터 간 거리를 n으로 유지하며 순회하면, 삭제할 노드의 이전 노드에 도달할 수 있다. [풀이.. 2025. 6. 30. [DSA][Linked List] 04. Reorder List LeetCode 143 04. Reorder ListYou are given the head of a singly linked-list. The list can be represented as:L0 → L1 → … → Ln - 1 → LnReorder the list to be on the following form:L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …You may not modify the values in the list's nodes. Only nodes themselves may be changed. [질문하기]- 리스트가 길이가 1 이하일 때는 어떻게 처리하나요?- 추가 공간 없이 in-place로 동작해야 하나요? [아이디어]- Fast and Slow .. 2025. 6. 29. Floyd’s Cycle Detection Algorithm Floyd’s Cycle Detection Algorithm은 연결 리스트에서 사이클의 존재 여부를 확인하고, 사이클이 존재할 경우 사이클의 시작 지점까지 찾아낼 수 있는 알고리즘이다. 이 알고리즘은 두 개의 포인터를 사용하는데, 하나는 한 칸씩(slow), 다른 하나는 두 칸씩(fast) 이동하며 진행된다. 이러한 구조 때문에 Fast and Slow Pointer Algorithm, 또는 Tortoise and Hare Algorithm(거북이와 토끼 알고리즘)이라고도 불린다. [알고리즘 구현]# Definition for singly-linked list node.class ListNode: def __init__(self, val=0, next=None): self.val = v.. 2025. 6. 28. 이전 1 2 3 4 ··· 18 다음