-
-
Notifications
You must be signed in to change notification settings - Fork 338
[gyeo-ri] WEEK 07 solutions #2552
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
65c7d88
feat: 반복문을 이용한 풀이
gyeo-ri 1fa2f2a
fix: 기존 로직 가독성 개선
gyeo-ri 705d43f
fix: 메모리 사용량 절감
gyeo-ri 0b94abb
fix: 재귀를 사용한 풀이
gyeo-ri f3179cb
feat: 결과 요약 작성
gyeo-ri c63c0cc
Merge pull request #7 from gyeo-ri/reverse-linked-list
gyeo-ri d6d3fa1
Merge branch 'DaleStudy:main' into main
gyeo-ri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """ | ||
| [결과 요약] | ||
| # 시도한 로직 수: 3 | ||
| 1. 반복문을 사용한 풀이: 코드는 짧고 성능은 좋지만 (새로운 노드 객체 정의를 위해) 불필요한 메모리 사용이 있어 (2)에서 개선함 | ||
| - 시간복잡도 O(n) / 공간복잡도 O(n) | ||
| 2. 반복문 풀이와 메모리 개선: 기존 노드(ListNode)의 연결 관계(next)를 직접 바꾸는 방식) | ||
| - 시간복잡도 O(n) / 공간복잡도 O(1) | ||
| 3. 재귀 풀이: 반복문과 시간복잡도는 동일하나 실제 실행 속도가 약간 느리고, 공간복잡도에서는 불리함 | ||
| - 재귀는 기본적으로 공간의 깊이에 비례하여 O(1)의 공간복잡도가 나오기는 어려움 | ||
| - 시간복잡도 O(n) / 공간복잡도 O(n) | ||
| """ | ||
|
|
||
|
|
||
| # Definition for singly-linked list. | ||
| class ListNode: | ||
| def __init__(self, val=0, next=None): | ||
| self.val: int = val | ||
| self.next: ListNode | None = next | ||
|
|
||
|
|
||
| # 재귀를 사용한 최종 풀이 | ||
| class Solution: | ||
| def reverseList(self, head: ListNode | None) -> ListNode | None: | ||
| # 1. 리스트가 비었거나(head is None) 마지막 노드일 때(head.next is None)는 종료 | ||
| if head is None or head.next is None: | ||
| return head | ||
|
|
||
| # 2. 현재 노드(=head)를 제외하고, 다음 노드(next_node)부터 끝까지를 재귀 호출(뒤쪽 리스트를 뒤집기) | ||
| next_node = head.next | ||
| reversed_node = self.reverseList(next_node) | ||
|
|
||
| # 3. 현재 노드를 뒤집힌 이전 노드(reversed_node)의 뒤에 붙이기 | ||
| # reversed_node의 가장 끝은 head.next이므로, head를 next_node 뒤에 붙인 다음 마지막 노드로 표시(head.next = None) | ||
| next_node.next = head | ||
| head.next = None | ||
|
|
||
| # 4. 최종 노드를 반환 | ||
| # 2의 reversed_node는 head(=마지막 노드)가 빠진 상태였지만 3에서 next_node -> head를 연결하면서 완성됨 | ||
| return reversed_node | ||
|
|
||
|
|
||
| """ | ||
| # 반복문을 사용한 풀이 | ||
| class Solution: | ||
| def reverseList(self, head: ListNode | None) -> ListNode | None: | ||
| current_node = head | ||
| reversed_node = None | ||
|
|
||
| while current_node: | ||
| next_node = current_node.next | ||
| current_node.next = reversed_node | ||
|
|
||
| if next_node is None: | ||
| return current_node | ||
|
|
||
| reversed_node = current_node | ||
| current_node = next_node | ||
| """ | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_cases = [ | ||
| ([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]), | ||
| ([1, 2], [2, 1]), | ||
| ([], []), | ||
| ] | ||
|
|
||
| solution = Solution() | ||
|
|
||
| for idx, (inp, expected) in enumerate(test_cases, start=1): | ||
| head = None | ||
| for value in reversed(inp): | ||
| head = ListNode(value, head) | ||
|
|
||
| result = solution.reverseList(head) | ||
|
|
||
| result_list = [] | ||
| while result is not None: | ||
| result_list.append(result.val) | ||
| result = result.next | ||
|
|
||
| assert ( | ||
| result_list == expected | ||
| ), f"Test Case {idx} Failed: Expected {expected}, Got {result_list}" | ||
|
|
||
| print("All test cases passed.") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.