forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaklee.py
126 lines (104 loc) ยท 4.47 KB
/
haklee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""TC: O(n^2), SC: O(1)
n์ ์ฃผ์ด์ง ๋ฆฌ์คํธ์ ๋
ธ๋ ๊ฐ์.
์์ด๋์ด:
์๋์ ์ ์ฐจ๋ฅผ ๋ฐ๋ณตํ๋ค.
- ๋ฆฌ์คํธ๊ฐ ์ฃผ์ด์ ธ์๋ค. ์ฌ๊ธฐ์ head๊ฐ ์กด์ฌํ๋ค. (*๋ก ํ์)
- 1 -> 2 -> 3 -> 4 -> 5
*
- ๋ฆฌ์คํธ์ ๋ ๋
ธ๋๋ฅผ ๋ผ์ด๋ธ๋ค.
- 1 -> 2 -> 3 -> 4 5
*
- head์ next๋ฅผ ๋ผ์ด๋ธ ๋
ธ๋๋ก ๋ฐ๊พผ๋ค.
- 1 2 -> 3 -> 4
*โโ> 5
- head์ next์ next๋ฅผ ์๋ head์ next์๋ ๋
ธ๋๋ก ๋ฐ๊พผ๋ค.
- 1 โโ> 2 -> 3 -> 4
*โโ> 5
- head๋ฅผ ์๋ก ๋ง๋ค์ด์ง head์ next์ next๋ก ๋ฐ๊พผ๋ค.
- 1 โโ> 2 -> 3 -> 4
โโ> 5 *
์ฝ๋์์๋ ์์ ์์ด๋์ด๋ฅผ cur_head, cur_next, end ๊ฐ์ ๋ณ์๋ฅผ ์จ์ ๊ตฌํํ๋ค. ์ด๋ end ๋
ธ๋๋ฅผ
๊ตฌํ๊ธฐ ์ํ ํจ์๋ฅผ ๋ฐ๋ก ๊ตฌํํ๋๋ฐ, ์์ธํ ๋ด์ฉ์ ์ฝ๋๋ฅผ ์ฐธ์กฐํ๋ฉด ๋๋ค.
SC:
- ๋ง์ง๋ง ๋
ธ๋๋ฅผ ๊ฐ์ ธ์ค๋ `pop_end` ํจ์์์ end, result ๋ณ์ ๊ด๋ฆฌ์ O(1).
- cur_head, cur_next, end ๋ฑ์ ๋ณ์ ๊ด๋ฆฌ์ O(1).
- ์ข
ํฉํ๋ฉด O(1).
TC:
- ํ ๋ฒ ๋ ๋
ธ๋๋ฅผ ๋ผ์ด์ head์ ๋ถ์ด๊ณ head์ next์๋ ๋
ธ๋๋ฅผ ๋ ๋
ธ๋์ ๋ถ์ด๋ ์ํ์ O(1)
- ... ์ธ ๊ฒ์ฒ๋ผ ๋ณด์ด์ง๋ง ๋ ๋
ธ๋๋ฅผ ๊ตฌํ๋ ๋ฐ์ head ๋ค์ ๋ฌ๋ฆฐ ๋
ธ๋ ๊ฐ์ ๋งํผ์ ์ํํด์ผ ํ๋ค.
- ์ฒ์ ๋ฆฌ์คํธ์ ๊ธธ์ด๊ฐ n์์ ์์ํด์ ํ ๋ฒ์ ์์
์ํ์ ํ์ํด์ผ ํ๋ ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ 2์ฉ ์ค์ด๋ ๋ค.
- n + (n-2) + ... = O(n^2). ์์ธํ ์ ์ ๋๋ ์๋ตํ๊ฒ ๋ค.
"""
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
def pop_end(node: Optional[ListNode]) -> Optional[ListNode]:
end = node
while end.next and end.next.next:
# end์ next์ next๊ฐ ์์ผ๋ฉด, ์ฆ, end์ next๊ฐ ๋ฆฌ์คํธ์ ๋์ด๋ฉด ๋ฉ์ถ๋ค.
# ์ด๋ฆ์ end๋ผ๊ณ ์ง์ด๋์์ ํท๊ฐ๋ฆด ์ ์๋๋ฐ, ์ฌ๊ธฐ์ ๊ตฌํ๊ณ ์ ํ๋ end๋
# ๋ค์์ ๋ ๋ฒ์งธ ๋
ธ๋๋ค...
end = end.next
# ์ฐ ๋ง์ง๋ง ๋
ธ๋๋ฅผ result์ ๋ฃ์ด๋๋๋ค.
result = end.next
# ์ฐ ๋ง์ง๋ง ๋
ธ๋๋ฅผ ๋ฆฌ์คํธ์์ ์ ๊ฑฐํ๋ค. ๋ง์ง๋ง์์ ๋ ๋ฒ์งธ ๋
ธ๋์ next๋ฅผ
# None์ผ๋ก ๋ฐ๊พธ๋ฉด ์ฐ๊ฒฐ์ด ๋์ด์ง.
end.next = None
# ์ฐ ๋ง์ง๋ง ๋
ธ๋๋ฅผ ๋ฆฌํด.
return result
cur_head = head
while cur_head and cur_head.next and cur_head.next.next:
cur_next = cur_head.next
end = pop_end(cur_head)
cur_head.next = end
end.next = cur_next
cur_head = end.next
"""TC: O(n), SC: O(n)
n์ ์ฃผ์ด์ง ๋ฆฌ์คํธ์ ๋
ธ๋ ๊ฐ์.
์์ด๋์ด:
- ์์ ์์ด๋์ด๋ ๋ค ์ข์๋ฐ ๋ ๋
ธ๋๋ฅผ ์ฐพ๊ธฐ ์ํด ๋ฆฌ์คํธ ์ ์ฒด๋ฅผ ์ํํด์ผ ํด์ TC๊ฐ ๋๋ฌด ์ปค์ง๋ค.
- ๋ฆฌ์คํธ ์ํ๋ฅผ ์ ํ๊ณ ์ถ์ผ๋ฉด ๋ฆฌ์คํธ์ ์๋ ๋
ธ๋ ๊ฐ์ ํ ๋ฒ ์ํํ๋ฉด์ ์ฃ๋ค ํ์ด์ฌ์ ๋ฆฌ์คํธ์
๋ฃ์ด๋๊ณ ํ์ํ ๊ฐ์ ๊บผ๋ด์ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๋ฉด ๋๋ ๊ฒ์ด ์๋๊น?
- ํ์ด์ฌ ๋ฆฌ์คํธ๋ ์ฃผ์ด์ง singly-linked list์๋ ๋ฌ๋ฆฌ ์ธ๋ฑ์ค๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ฏ๋ก, ํฌํฌ์ธํฐ๋ฅผ ์จ์
์๋ก ๊ฒฐ๊ณผ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด์ ๋ฆฌํดํ์.
SC:
- ์ฃผ์ด์ง singly-linked list๋ฅผ ์ํํ๋ฉด์ ๊ฐ์ ๋ฝ์์ ํ์ด์ฌ ๋ฆฌ์คํธ์ ๋ฃ์. O(n).
TC:
- ํฌํฌ์ธํฐ๋ฅผ ์จ์ ๋ชจ๋ ์์ดํ
์ ํ ๋ฒ์ฉ๋ง ์ ๊ทผํ์ฌ ์๋ก ๋
ธ๋ ๋ง๋ฆ. O(n).
"""
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if not head:
return
nums = []
cur_head = head
while cur_head:
nums.append(cur_head.val)
cur_head = cur_head.next
head.val = nums[0]
cur_head = head
ptr = [1, len(nums) - 1]
i = 1
while ptr[0] <= ptr[1]:
cur_head.next = ListNode(nums[ptr[i]])
cur_head = cur_head.next
if i == 0:
ptr[i] += 1
else:
ptr[i] -= 1
i = (i + 1) % 2