File tree 1 file changed +45
-0
lines changed
remove-nth-node-from-end-of-list 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
+ * 2์ฐจ
3
+ * n๋งํผ first๊ฐ ์์ง์ธ ๋ค์, first๊ฐ ๋๊น์ง ๊ฐ๋๊น์ง second๋ฅผ ์์ง์
๋๋ค.
4
+ * ๊ทธ๋ผ ๊ทธ ์์น๊ฐ ๋์์ n๋ฒ์งธ์์ ์ ์ ์๋ ํ์ด์
๋๋ค.
5
+ *
6
+ * TC: O(N)
7
+ * SC: O(1)
8
+ * N: list length
9
+ */
10
+
11
+ /**
12
+ * Definition for singly-linked list.
13
+ * function ListNode(val, next) {
14
+ * this.val = (val===undefined ? 0 : val)
15
+ * this.next = (next===undefined ? null : next)
16
+ * }
17
+ */
18
+ /**
19
+ * @param {ListNode } head
20
+ * @param {number } n
21
+ * @return {ListNode }
22
+ */
23
+ var removeNthFromEnd = function ( head , n ) {
24
+ const resultHead = new ListNode ( null , head ) ;
25
+ let first = resultHead ;
26
+ let second = resultHead ;
27
+
28
+ while ( n > 0 ) {
29
+ first = first . next ;
30
+ n -= 1 ;
31
+ }
32
+
33
+ while ( first . next ) {
34
+ first = first . next ;
35
+ second = second . next ;
36
+ }
37
+
38
+ second . next = second . next . next ;
39
+
40
+ return resultHead . next ;
41
+ } ;
42
+
43
+ /**
44
+ * 1์ฐจ
45
+ * ์ ์ฒด ์ํ๋ก ๊ฐฏ์๋ฅผ ํ์
ํ๊ณ ํด๋น ์์น๋ก๊ฐ์ ๋งํฌ ์ฐ๊ฒฐ ์์ ์์
46
+ *
2
47
* TC: O(N)
3
48
* SC: O(1)
4
49
* N: list length
You canโt perform that action at this time.
0 commit comments