Skip to content

[KwonNayeon] Week 12 solutions #1593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions remove-nth-node-from-end-of-list/KwonNayeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
- 추가 공간을 사용하지 않음, 정해진 개수의 변수만 사용

풀이방법:
1. fast 포인터를 n번 앞으로 이동
2. base case: 만약 fast가 None이라면, 첫 번째 노드를 제거함
- 투 포인터 기법: 두 포인터 사이의 고정된 간격(n)을 이용하여 fast가 끝에 도달했을 때 slow가 정확한 위치에 있도록 함
1. fast 포인터를 n번 이동시켜 slow와 n칸 간격을 만듦
2. Base case: fast가 None이면 첫 번째 노드를 삭제 (head.next 반환)
3. fast.next가 None일 때까지 두 포인터를 함께 이동
4. slow의 다음 노드를 제거함 (slow.next = slow.next.next로 연결을 끊어냄)
-> 이때 slow는 삭제할 노드의 바로 앞 위치에 도달
4. slow.next = slow.next.next로 삭제할 노드를 건너뛰도록 연결 변경
"""
# Definition for singly-linked list.
# class ListNode:
Expand Down
80 changes: 29 additions & 51 deletions reorder-list/KwonNayeon.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드의 동작 과정을 테스트 케이스를 활용해서 주석으로 꼼꼼하게 작성해주셔서 전체적인 코드 흐름을 파악하는데 많이 도움이 되는 것 같습니다. 이전에도 몇 번 코드 리뷰를 진행했던 적이 있었는데, 그때마다 꼼꼼하게 주석 및 풀이 도출 과정을 적으신 부분이 굉장히 인상적이었는데, 꾸준히 지속하시는 모습이 멋져요. :)

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- 포인터들을 번갈아가며 연결함

노트:
- 포인터 조작(연결리스트 뒤집기, 병합) 방법을 다 까먹어서 복습용 예시 주석을 추가해둠
- 포인터 조작(연결리스트 뒤집기, 병합)이 어려움. 스택 풀이도 준비해두기.
"""
# Definition for singly-linked list.
# class ListNode:
Expand All @@ -31,60 +31,38 @@
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
# 초기 상태: 1->2->3->4->5

"""
Do not return anything, modify head in-place instead.
"""
# 1단계: 중간 지점 찾기
slow = head # slow = 1
fast = head # fast = 1
slow = head
fast = head

while fast and fast.next:
slow = slow.next # slow: 1->2->3
fast = fast.next.next # fast: 1->3->5->None
# 결과: slow는 3에 위치

slow = slow.next
fast = fast.next.next

# 2단계: 뒷부분 뒤집기
prev = None # prev = None
curr = slow.next # curr = 4 (뒷부분 시작점)
prev = None
curr = slow.next
slow.next = None # 분리: 1->2->3 | 4->5

while curr:
# 1회전: curr=4, prev=None
next_temp = curr.next # next_temp = 5
curr.next = prev # 4->None
prev = curr # prev = 4
curr = next_temp # curr = 5
# 상태: 1->2->3 | 4->None, curr=5

# 2회전: curr=5, prev=4
next_temp = curr.next # next_temp = None
curr.next = prev # 5->4
prev = curr # prev = 5
curr = next_temp # curr = None (종료)
# 상태: 1->2->3 | 5->4->None

next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp

# 3단계: 앞부분과 뒷부분 합치기
first = head # first = 1->2->3
second = prev # second = 5->4
first = head
second = prev

while second:
# 1회전: first=1, second=5
temp1 = first.next # temp1 = 2
temp2 = second.next # temp2 = 4

first.next = second # 1->5
second.next = temp1 # 5->2
# 현재 상태: 1->5->2->3, 남은 second = 4

first = temp1 # first = 2
second = temp2 # second = 4

# 2회전: first=2, second=4
temp1 = first.next # temp1 = 3
temp2 = second.next # temp2 = None

first.next = second # 2->4
second.next = temp1 # 4->3
# 현재 상태: 1->5->2->4->3

first = temp1 # first = 3
second = temp2 # second = None (종료)

# 최종 결과: 1->5->2->4->3
temp1 = first.next
temp2 = second.next

first.next = second
second.next = temp1

first = temp1
second = temp2
6 changes: 5 additions & 1 deletion reverse-linked-list/KwonNayeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
curr = head

while curr is not None:
# while문 조건 - 마지막 노드도 처리해야 함
while curr:

temp = curr.next
curr.next = prev

# prev: 지금까지 뒤집어진 부분의 시작점
# curr: 아직 처리 안 한 부분의 시작점
prev = curr
curr = temp

Expand Down
5 changes: 2 additions & 3 deletions same-tree/KwonNayeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
풀이방법:
1. DFS와 재귀를 활용하여 두 트리를 동시에 탐색
2. base case:
- p와 q가 모두 None이면 → 같은 트리
- 둘 중 하나만 None이거나 노드의 값이 다르면 → 다른 트리
- p와 q가 모두 None일 때: 같은 트리
- 둘 중 하나만 None이거나 노드의 값이 다를 때: 다른 트리
3. 재귀로 왼쪽과 오른쪽 서브트리가 모두 같은지 확인
"""
# Definition for a binary tree node.
Expand All @@ -31,4 +31,3 @@ def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
return False

return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)