Skip to content

Commit 0eef222

Browse files
committed
feat: reorder-list
1 parent 23f1c5f commit 0eef222

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

reorder-list/std-freejia.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def reorderList(self, head: Optional[ListNode]) -> None:
3+
if not head or not head.next:
4+
return
5+
# 중간 노드 찾기
6+
slow, fast = head, head
7+
while fast and fast.next:
8+
slow = slow.next # 1칸
9+
fast = fast.next.next # 2칸
10+
11+
# 뒤 구간들을 뒤집는다
12+
prev, cur = None, slow.next
13+
# 중간노드 기준으로 앞 구간만 떼어놓는다
14+
slow.next = None
15+
16+
while cur:
17+
nxt = cur.next # 다음 노드 기억
18+
cur.next = prev
19+
prev = cur
20+
cur = nxt
21+
# 앞, 뒤 구간들을 병합
22+
first, second = head, prev
23+
while second:
24+
n1, n2 = first.next, second.next
25+
first.next = second
26+
second.next = n1
27+
first, second = n1, n2
28+
29+

0 commit comments

Comments
 (0)