File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ package leetcode_study
2+
3+ /*
4+ * singly linked list๋ฅผ ์ฌ์ ๋ ฌํ๋ ๋ฌธ์
5+ * ์๊ฐ ๋ณต์ก๋: O(n)
6+ * -> ์ ์ฒด ๋
ธ๋๋ฅผ ์ ์ฅํ๊ธฐ ์ํด ์ํํ๋ ๊ณผ์ O(n)
7+ * -> ๋ฆฌ์คํธ์ ์๊ณผ ๋ค์ ํฌ์ธํฐ๋ฅผ ๋๊ณ ์ฃผ์๊ฐ์ ๋ณ๊ฒฝํ๋ ๊ณผ์ O(log n)
8+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n)
9+ * -> ๋
ธ๋ ์ ์ฒด๋ฅผ ๋ด์ ์๋ก์ด list ํ์ O(n)
10+ * */
11+ fun reorderList (head : ListNode ? ): Unit {
12+ val tempNodeList = mutableListOf<ListNode >()
13+ var currentNode = head
14+
15+ while (currentNode != null ) {
16+ tempNodeList.add(currentNode)
17+ currentNode = currentNode.next
18+ }
19+
20+ // ์์ชฝ ๋์์๋ถํฐ ๊ต์ฐจ๋ก ์ฐ๊ฒฐ
21+ var i = 0
22+ var j = tempNodeList.size - 1
23+ while (i < j) {
24+ // ๋จผ์ ์์ชฝ ๋
ธ๋์ next๊ฐ ๋ค์ชฝ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํจ
25+ tempNodeList[i].next = tempNodeList[j]
26+ i++ // ๋ค์ ์์ชฝ ๋
ธ๋ ์ ํ
27+
28+ // ๋ง์ฝ ์์ชฝ๊ณผ ๋ค์ชฝ์ด ๋ง๋ ๊ฒฝ์ฐ (์ง์๊ฐ์ผ ๋), ๋ฐ๋ณต ์ข
๋ฃ
29+ if (i == j) break
30+
31+ // ๋ค์ชฝ ๋
ธ๋์ next๊ฐ ์๋ก์ด ์์ชฝ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํจ
32+ tempNodeList[j].next = tempNodeList[i]
33+ j-- // ๋ค์ ๋ค์ชฝ ๋
ธ๋ ์ ํ
34+ }
35+ tempNodeList[i].next = null
36+ }
You canโt perform that action at this time.
0 commit comments