Skip to content

Commit 3dc6c79

Browse files
donghyeon95donghyeon95
authored andcommitted
feat: Remove Nth Node From End of List #248
1 parent f73dfd7 commit 3dc6c79

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.ArrayDeque;
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.Queue;
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* public class ListNode {
9+
* int val;
10+
* ListNode next;
11+
* ListNode() {}
12+
* ListNode(int val) { this.val = val; }
13+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
14+
* }
15+
*/
16+
class Solution {
17+
public ListNode removeNthFromEnd(ListNode head, int n) {
18+
ArrayList<ListNode> list = new ArrayList<>();
19+
ListNode nhead = head;
20+
21+
while (nhead != null) {
22+
list.add(nhead);
23+
nhead = nhead.next;
24+
}
25+
26+
int size = list.size();
27+
28+
if (size == n) {
29+
return head.next;
30+
}
31+
32+
list.get(size - n - 1).next = list.get(size - n).next;
33+
34+
return head;
35+
}
36+
}
37+

0 commit comments

Comments
 (0)