Skip to content

Commit 854537f

Browse files
committed
Added detailed comments for pointer manipulation review
1 parent 0bcef4a commit 854537f

File tree

1 file changed

+62
-35
lines changed

1 file changed

+62
-35
lines changed

โ€Žreorder-list/KwonNayeon.py

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
- ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์ˆœํšŒํ•˜๋ฉด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ฐ ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•จ
88
99
Space Complexity: O(1)
10-
- ์ •ํ•ด์ง„ ๊ฐœ์ˆ˜์˜ ๋ณ€์ˆ˜ ์™ธ์—๋Š” ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
10+
- ์ •ํ•ด์ง„ ๋ณ€์ˆ˜ ์™ธ์—๋Š” ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
1111
1212
ํ’€์ด๋ฐฉ๋ฒ•:
1313
1. ์ค‘๊ฐ„ ์ง€์  ์ฐพ๊ธฐ
14-
- slow/fast ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๊ฐ„ ์ง€์  ์ฐพ๊ธฐ
14+
- slow/fast ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๊ฐ„ ์ง€์  ์ฐพ๊ธฐ
1515
2. ๋’ท๋ถ€๋ถ„ ๋’ค์ง‘๊ธฐ
16-
- prev, curr ํฌ์ธํ„ฐ๋กœ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฉํ–ฅ ์ „ํ™˜
17-
- next_temp์— ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•œ ํ›„ ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
16+
- prev, curr ํฌ์ธํ„ฐ๋กœ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฉํ–ฅ ์ „ํ™˜
17+
- next_temp์— ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•œ ํ›„ ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
1818
3. ์•ž๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„ ํ•ฉ์น˜๊ธฐ
19-
- ๋‘ ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘์ (first, second)๋ถ€ํ„ฐ ์‹œ์ž‘
20-
- temp1, temp2์— ๋‹ค์Œ ๋…ธ๋“œ ์ €์žฅ
21-
- ํฌ์ธํ„ฐ๋“ค์„ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉฐ ์—ฐ๊ฒฐํ•จ
19+
- ๋‘ ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘์ (first, second)๋ถ€ํ„ฐ ์‹œ์ž‘
20+
- temp1, temp2์— ๋‹ค์Œ ๋…ธ๋“œ ์ €์žฅ
21+
- ํฌ์ธํ„ฐ๋“ค์„ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉฐ ์—ฐ๊ฒฐํ•จ
22+
23+
๋…ธํŠธ:
24+
- ํฌ์ธํ„ฐ ์กฐ์ž‘(์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋’ค์ง‘๊ธฐ, ๋ณ‘ํ•ฉ) ๋ฐฉ๋ฒ•์„ ๋‹ค ๊นŒ๋จน์–ด์„œ ๋ณต์Šต์šฉ ์˜ˆ์‹œ ์ฃผ์„์„ ์ถ”๊ฐ€ํ•ด๋‘ 
2225
"""
2326
# Definition for singly-linked list.
2427
# class ListNode:
@@ -27,37 +30,61 @@
2730
# self.next = next
2831
class Solution:
2932
def reorderList(self, head: Optional[ListNode]) -> None:
30-
"""
31-
Do not return anything, modify head in-place instead.
32-
"""
33-
# ์ค‘๊ฐ„ ์ง€์  ์ฐพ๊ธฐ
34-
slow = head
35-
fast = head
33+
# ์ดˆ๊ธฐ ์ƒํƒœ: 1->2->3->4->5
34+
35+
# 1๋‹จ๊ณ„: ์ค‘๊ฐ„ ์ง€์  ์ฐพ๊ธฐ
36+
slow = head # slow = 1
37+
fast = head # fast = 1
3638
while fast and fast.next:
37-
slow = slow.next
38-
fast = fast.next.next
39-
40-
# ๋’ท๋ถ€๋ถ„ ๋’ค์ง‘๊ธฐ
41-
prev = None
42-
curr = slow.next
43-
slow.next = None
39+
slow = slow.next # slow: 1->2->3
40+
fast = fast.next.next # fast: 1->3->5->None
41+
# ๊ฒฐ๊ณผ: slow๋Š” 3์— ์œ„์น˜
42+
43+
# 2๋‹จ๊ณ„: ๋’ท๋ถ€๋ถ„ ๋’ค์ง‘๊ธฐ
44+
prev = None # prev = None
45+
curr = slow.next # curr = 4 (๋’ท๋ถ€๋ถ„ ์‹œ์ž‘์ )
46+
slow.next = None # ๋ถ„๋ฆฌ: 1->2->3 | 4->5
47+
4448
while curr:
45-
next_temp = curr.next
46-
curr.next = prev
47-
prev = curr
48-
curr = next_temp
49+
# 1ํšŒ์ „: curr=4, prev=None
50+
next_temp = curr.next # next_temp = 5
51+
curr.next = prev # 4->None
52+
prev = curr # prev = 4
53+
curr = next_temp # curr = 5
54+
# ์ƒํƒœ: 1->2->3 | 4->None, curr=5
4955

50-
# ์•ž๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„ ํ•ฉ์น˜๊ธฐ
51-
first = head
52-
second = prev
56+
# 2ํšŒ์ „: curr=5, prev=4
57+
next_temp = curr.next # next_temp = None
58+
curr.next = prev # 5->4
59+
prev = curr # prev = 5
60+
curr = next_temp # curr = None (์ข…๋ฃŒ)
61+
# ์ƒํƒœ: 1->2->3 | 5->4->None
62+
63+
# 3๋‹จ๊ณ„: ์•ž๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„ ํ•ฉ์น˜๊ธฐ
64+
first = head # first = 1->2->3
65+
second = prev # second = 5->4
66+
5367
while second:
54-
temp1 = first.next
55-
temp2 = second.next
68+
# 1ํšŒ์ „: first=1, second=5
69+
temp1 = first.next # temp1 = 2
70+
temp2 = second.next # temp2 = 4
5671

57-
first.next = second
58-
second.next = temp1
72+
first.next = second # 1->5
73+
second.next = temp1 # 5->2
74+
# ํ˜„์žฌ ์ƒํƒœ: 1->5->2->3, ๋‚จ์€ second = 4
5975

60-
first = temp1
61-
second = temp2
62-
63-
76+
first = temp1 # first = 2
77+
second = temp2 # second = 4
78+
79+
# 2ํšŒ์ „: first=2, second=4
80+
temp1 = first.next # temp1 = 3
81+
temp2 = second.next # temp2 = None
82+
83+
first.next = second # 2->4
84+
second.next = temp1 # 4->3
85+
# ํ˜„์žฌ ์ƒํƒœ: 1->5->2->4->3
86+
87+
first = temp1 # first = 3
88+
second = temp2 # second = None (์ข…๋ฃŒ)
89+
90+
# ์ตœ์ข… ๊ฒฐ๊ณผ: 1->5->2->4->3

0 commit comments

Comments
ย (0)