Karleb
Posted on
#2487. Remove Nodes From Linked List
https://leetcode.com/problems/remove-nodes-from-linked-list/?envType=daily-question&envId=2024-05-06
/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } *//** * @param {ListNode} head * @return {ListNode} */varremoveNodes=function(head){letstack=[]letcurr=headwhile(curr){while(stack.length&&curr.val>stack[stack.length-1]){stack.pop()}stack.push(curr.val)curr=curr.next}//This converts the stack into a linkedlistletdummy=newListNode()letdummyCurr=dummyfor(letvalofstack){dummyCurr.next=newListNode(val)dummyCurr=dummyCurr.next}returndummy.next};
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse