Skip to content

Commit 8904e80

Browse files
committed
solve remove nth node frome end of list
1 parent 0ba3e14 commit 8904e80

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public ListNode removeNthFromEnd(ListNode head, int n) {
3+
Queue<ListNode> queue = new LinkedList<>();
4+
ListNode temp = new ListNode(0, head);
5+
ListNode node = temp;
6+
7+
for (int i = 0; i < n + 1; i++) {
8+
queue.offer(node);
9+
node = node.next;
10+
}
11+
12+
while (node != null) {
13+
queue.poll();
14+
queue.offer(node);
15+
node = node.next;
16+
}
17+
18+
ListNode prev = queue.poll();
19+
prev.next = prev.next.next;
20+
21+
return temp.next;
22+
}
23+
}
24+

0 commit comments

Comments
 (0)