File tree 1 file changed +28
-0
lines changed
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ /*
4
+ * Node๋ฅผ ์ญ์์ผ๋ก ๋ง๋๋ ๋ฌธ์
5
+ * ์ถ๊ฐ ์ ์ฅ ๊ณต๊ฐ์ ์ฌ์ฉํด ๋
ธ๋๋ฅผ ์ฑ์ ๋ ๋ค ์ญํํ๋ฉด์ ํฌ์ธํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ผ๋ก ํด๊ฒฐ
6
+ * ์๊ฐ ๋ณต์ก๋: O(n)
7
+ * -> ์๋ก์ด ๊ณต๊ฐ์ ๊ฐ์ ์ถ๊ฐํ๋ ๋ฐ๋ณต๋ฌธ: O(n)
8
+ * -> ๊ฐ ๋
ธ๋์ ์ ๊ทผํ์ฌ ํฌ์ธํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๋ฐ๋ณต๋ฌธ: O(n)
9
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n)
10
+ * -> ์ฃผ์ด์ง ๋
ธ๋๋ฅผ ์ ์ฅํ ์ ์์๋งํผ ์ ์ฅ ๊ณต๊ฐ ํ์: O(n)
11
+ * */
12
+ fun reverseList (head : ListNode ? ): ListNode ? {
13
+ val stack = mutableListOf<ListNode >()
14
+ var currentNode = head
15
+ if (head == null ) return null
16
+
17
+ while (currentNode != null ) {
18
+ stack.add(currentNode)
19
+ currentNode = currentNode.next
20
+ }
21
+
22
+
23
+ for (i in stack.size - 1 downTo 1 ) {
24
+ stack[i].next = stack[i- 1 ]
25
+ }
26
+ stack[0 ].next = null
27
+ return stack[stack.size - 1 ]
28
+ }
You canโt perform that action at this time.
0 commit comments