File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ /**
3
+ * Definition for singly-linked list.
4
+ * function ListNode(val, next) {
5
+ * this.val = (val===undefined ? 0 : val)
6
+ * this.next = (next===undefined ? null : next)
7
+ * }
8
+ */
9
+ /**
10
+ * @param {ListNode } head
11
+ * @return {ListNode }
12
+ */
13
+ var reverseList = function ( head ) {
14
+ if ( head === null ) {
15
+ return null ;
16
+ }
17
+
18
+ if ( head . next === null ) {
19
+ return head ;
20
+ }
21
+
22
+ const stack = [ ] ;
23
+
24
+ let nextNode = head ;
25
+
26
+ while ( nextNode ) {
27
+ stack . push ( nextNode ) ;
28
+
29
+ nextNode = nextNode . next ;
30
+ }
31
+
32
+ for ( let i = stack . length - 1 ; i >= 0 ; i -- ) {
33
+ if ( i === 0 ) {
34
+ stack [ i ] . next = null ;
35
+ } else {
36
+ stack [ i ] . next = stack [ i - 1 ] ;
37
+ }
38
+ }
39
+
40
+ return stack [ stack . length - 1 ] ;
41
+ } ;
42
+
43
+ // 시간복잡도 O(2n)
You can’t perform that action at this time.
0 commit comments