File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
remove-nth-node-from-end-of-list Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * public class ListNode {
4+ * int val;
5+ * ListNode next;
6+ * ListNode() {}
7+ * ListNode(int val) { this.val = val; }
8+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+ * }
10+ */
11+ class Solution {
12+ public ListNode removeNthFromEnd (ListNode head , int n ) {
13+
14+ // length 1์ธ ๊ฒฝ์ฐ๋ฅผ ์ํด temp ์์ฑ
15+ ListNode temp = new ListNode (0 );
16+ temp .next = head ;
17+
18+ // ํฌ ํฌ์ธํฐ ์ ์ธ
19+ ListNode fast = temp ;
20+ ListNode slow = temp ;
21+
22+ // n + 1์นธ๋งํผ fast ๋จผ์ ์ด๋
23+ for (int i = 0 ; i < n + 1 ; i ++) {
24+ fast = fast .next ;
25+ }
26+
27+ while (fast != null ) {
28+ fast = fast .next ;
29+ // ๋์ด์ง๋ ๋
ธ๋ ๋ฐ๋ก ์๊น์ง ์ด๋
30+ slow = slow .next ;
31+ }
32+
33+ // slow.next = ๋์ด์ ธ์ ์นํํด์ผ ํ๋ ์์น
34+ slow .next = slow .next .next ;
35+
36+ return temp .next ;
37+ }
38+ }
39+
You canโt perform that action at this time.
0 commit comments