Skip to content

Commit b0d7f1b

Browse files
committed
solve 1
1 parent 9d995c6 commit b0d7f1b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
3+
- ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
4+
5+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
6+
- ์žฌ๊ท€ ํ˜ธ์ถœ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ํ˜ธ์ถœ ์Šคํƒ์— ์ตœ๋Œ€ n๋ฒˆ์˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์Œ“์ด๋ฏ€๋กœ ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
7+
'''
8+
from typing import Optional
9+
10+
class ListNode:
11+
def __init__(self, val=0, next=None):
12+
self.val = val
13+
self.next = next
14+
15+
class Solution:
16+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
17+
if not head or not head.next:
18+
return head
19+
20+
new_head = self.reverseList(head.next) # find the last node
21+
head.next.next = head # reverse
22+
head.next = None # remove cycle
23+
24+
return new_head

0 commit comments

Comments
ย (0)