File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
remove-nth-node-from-end-of-list Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments