File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @return {void } Do not return anything, modify head in-place instead.
11
+ */
12
+ var reorderList = function ( head ) {
13
+ const nodes = { } ;
14
+ let current = head ;
15
+ let n = 0 ; // ์ธ๋ฑ์ค ์ญํ
16
+
17
+ // ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉด์ ๊ฐ ๋
ธ๋๋ฅผ ๊ฐ์ฒด์ ์ ์ฅ
18
+ while ( current ) {
19
+ nodes [ n ] = current ;
20
+ current = current . next ;
21
+ nodes [ n ] . next = null ; // ์ฌ๊ธฐ์ ๋ชจ๋ ๋
ธ๋์ next๋ฅผ null๋ก ๋ณ๊ฒฝ
22
+ // ์ด๋ ๊ฒ ์ ํ๋ฉด next ๋ฐ๊พธ๊ธฐ ํ ๋ค์์ ๋งจ ๋ง์ง๋ง ๋
ธ๋์ next๋ง null๋ก ๋ฐ๊ฟ์ผ ํจ
23
+ n ++
24
+ }
25
+
26
+ // ์ ์ฅํ ๋
ธ๋๋ฅผ i, n-i, ... ์์๋ก ์ ๊ทผํ๋ฉด์ next ๋ฐ๊พธ๊ธฐ
27
+ for ( let i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) {
28
+ nodes [ i ] . next = nodes [ n - i - 1 ] ;
29
+
30
+ // n-i-1 === i+1 ์ธ ๊ฒฝ์ฐ, node.next = node ์ฒ๋ผ ์
ํ ์ํ์ด ๋ ์ ์์
31
+ if ( n - i - 1 !== i + 1 ) {
32
+ nodes [ n - i - 1 ] . next = nodes [ i + 1 ] ;
33
+ }
34
+ }
35
+ } ;
36
+
37
+ // ์๊ฐ๋ณต์ก๋: O(n)
38
+ // ๊ณต๊ฐ๋ณต์ก๋: O(n)
You canโt perform that action at this time.
0 commit comments