Skip to content
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

[Lyla] Week 07 #926

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions reverse-linked-list/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
시간 복잡도: O(n)
- 리스트의 모든 노드를 한 번씩 방문하므로 시간 복잡도는 O(n)입니다.

공간 복잡도: O(n)
- 재귀 호출을 사용하므로 호출 스택에 최대 n번의 함수 호출이 쌓이므로 공간 복잡도는 O(n)입니다.
'''
from typing import Optional

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]:
if not head or not head.next:
return head

new_head = self.reverseList(head.next) # find the last node
head.next.next = head # reverse
head.next = None # remove cycle

return new_head
Loading