File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def reorderList (self , head : Optional [ListNode ]) -> None :
3+ """
4+ Intuition:
5+ nodes ๋ฆฌ์คํธ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ์ ์ฅํ๋ค.
6+ ํ์ฌ ๋
ธ๋๊ฐ -i - 1๋ฒ์งธ ๋
ธ๋์ผ ๋,
7+ ํ์ฌ ๋
ธ๋์ next๋ head์ next๋ก ์ค์ ํ๊ณ ,
8+ head์ next๋ ํ์ฌ ๋
ธ๋๋ก ์ค์ ํ๋ค.
9+ ์ดํ ๋ง์ง๋ง์ ์ค๊ฐ์ ์๋ ๋
ธ๋๋ฅผ None์ผ๋ก ์ค์ ํ๋ค.
10+
11+ Time Complexity:
12+ O(N):
13+ ๋ชจ๋ ๋
ธ๋๋ฅผ ์ํํ๋ฏ๋ก O(N)์ด๋ค.
14+
15+ Space Complexity:
16+ O(N):
17+ ๋ชจ๋ ๋
ธ๋๋ฅผ nodes ๋ฆฌ์คํธ์ ์ ์ฅํ๋ฏ๋ก O(N)์ด๋ค.
18+ """
19+ nodes = []
20+ node = head
21+ while node :
22+ nodes .append (node )
23+ node = node .next
24+
25+ for i in range ((len (nodes ) - 1 ) // 2 ):
26+ cur = nodes [- i - 1 ]
27+ cur .next = head .next
28+ head .next = cur
29+ head = cur .next
30+
31+ nodes [len (nodes ) // 2 ].next = None
You canโt perform that action at this time.
0 commit comments