We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c142276 commit 7e0e8cdCopy full SHA for 7e0e8cd
src/Monotonic_Stack/Problem_3_Nodes_From_Linked_List.java
@@ -0,0 +1,32 @@
1
+package Monotonic_Stack;
2
+
3
+// Problem Statement: Remove Nodes From Linked List (easy)
4
+// LeetCode Question: 2487. Remove Nodes From Linked List
5
6
+import java.util.Stack;
7
8
+class ListNode {
9
+ int val;
10
+ ListNode next;
11
+ ListNode(int val){
12
+ this.val = val;
13
+ }
14
+}
15
+public class Problem_3_Nodes_From_Linked_List {
16
17
+ public ListNode removeNodes(ListNode head){
18
+ Stack<ListNode> stack = new Stack<>();
19
+ ListNode cur = head;
20
+ while (cur != null) {
21
+ while (!stack.isEmpty() && stack.peek().val < cur.val){
22
+ stack.pop();
23
24
+ if (!stack.isEmpty()) {
25
+ stack.peek().next = cur;
26
27
+ stack.push(cur);
28
+ cur = cur.next;
29
30
+ return stack.isEmpty() ? null : stack.get(0);
31
32
0 commit comments