Skip to content

Commit

Permalink
Create RemoveNodesFroLinkedList.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahesh-addagatla authored May 6, 2024
1 parent b8a5b94 commit 928ba63
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions RemoveNodesFroLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public ListNode removeNodes(ListNode head) {
ListNode cur=head;
Stack<ListNode> st=new Stack<>();
while(cur!=null){
while(!st.isEmpty() && st.peek().val<cur.val){
st.pop();
}
st.push(cur);
cur=cur.next;
}
ListNode nex=null;

while(!st.isEmpty()){
cur=st.pop();
cur.next=nex;
nex=cur;
}
return cur;
}
}

0 comments on commit 928ba63

Please sign in to comment.