Skip to content
Open
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
18 changes: 18 additions & 0 deletions reverse-linked-list/freemjstudio.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

reverse-linked-list/freemjstudio.py
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev= None
        current = head

        while current:
            next_node = current.next
            current.next = prev

            prev = current
            current = next_node

        return prev
  • 패턴: Two Pointers, Linked List
  • 설명: 리스트를 역순으로 뒤집는 문제로, 포인터를 서로 교차시키며 앞뒤 연결을 바꿔나가는 전형적인 Two Pointers 패턴에 해당합니다. 순회와 포인터 갱신을 통해 한 방향으로 진행합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 현재 구현은 루프를 한 번 순회하며 각 노드의 next 포인터를 이전 노드로 바꿔 반전합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev= None
current = head

while current:
next_node = current.next
current.next = prev

prev = current
current = next_node

return prev
Loading