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
27 changes: 27 additions & 0 deletions reverse-linked-list/jylee2033.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Linked List
  • 설명: 이 코드는 단일 연결 리스트를 뒤집는 문제로, 리스트 노드의 연결 방향을 역전시키는 과정이 핵심입니다. 일반적인 연결 리스트 조작 패턴에 속하며, 특별한 알고리즘 패턴은 명확히 해당되지 않습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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]:
# Handle empty list
if head is None:
return head

prev = None
cur = head # Start from the head node

# Traverse until the last node
while cur.next is not None:
nxt = cur.next # Store next node
cur.next = prev # Reverse the link
prev = cur # Move prev forward
cur = nxt # Move cur forward

# Handle the last node
cur.next = prev
return cur

# Time Complexity: O(n)
# Space Complexity: O(1)
Loading