Skip to content

Commit fb26cbd

Browse files
committed
Reverse Linked List
1 parent 49d1861 commit fb26cbd

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed

โ€Žreverse-linked-list/socow.pyโ€Ž

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
- ์˜ˆ: 1โ†’2โ†’3โ†’4โ†’5 โ†’ 5โ†’4โ†’3โ†’2โ†’1
77
88
๐ŸŽฏ ํ•ต์‹ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜
9-
- ํŒจํ„ด: ๋ฐ˜๋ณต (Iterative) / ์žฌ๊ท€ (Recursive)
9+
- ํŒจํ„ด: ๋ฐ˜๋ณต (Iterative)
1010
- ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
11-
- ๊ณต๊ฐ„๋ณต์žก๋„: O(1) (๋ฐ˜๋ณต) / O(n) (์žฌ๊ท€ - ์ฝœ์Šคํƒ)
11+
- ๊ณต๊ฐ„๋ณต์žก๋„: O(1) (๋ฐ˜๋ณต)
1212
1313
๐Ÿ’ก ํ•ต์‹ฌ ์•„์ด๋””์–ด
1414
1. prev = None, curr = head๋กœ ์‹œ์ž‘
@@ -39,21 +39,3 @@ def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
3939
curr = next_node # curr ์ด๋™
4040

4141
return prev
42-
43-
44-
# ์žฌ๊ท€ ๋ฐฉ์‹ (Recursive)
45-
class SolutionRecursive:
46-
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
47-
# Base case: ๋นˆ ๋ฆฌ์ŠคํŠธ ๋˜๋Š” ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ
48-
if not head or not head.next:
49-
return head
50-
51-
# ์žฌ๊ท€: ๋‚˜๋จธ์ง€ ๋ฆฌ์ŠคํŠธ ๋’ค์ง‘๊ธฐ
52-
new_head = self.reverseList(head.next)
53-
54-
# ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ๋‚˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•จ
55-
head.next.next = head
56-
head.next = None
57-
58-
return new_head
59-

0 commit comments

Comments
ย (0)