LeetCode 2487. 从链表中移除节点 C#
原题链接
中等
作者:
hpstory
,
2022-11-29 10:38:04
,
所有人可见
,
阅读 213
C# 递归代码
public class Solution {
public ListNode RemoveNodes(ListNode head) {
if (head.next == null) return head;
ListNode node = RemoveNodes(head.next);
if (node.val > head.val) return node;
head.next = node;
return head;
}
}
C# 单调队列代码
public class Solution {
public ListNode RemoveNodes(ListNode head) {
LinkedList<ListNode> queue = new LinkedList<ListNode>();
while (head != null){
while (queue.Count > 0 && queue.Last.Value.val < head.val){
queue.RemoveLast();
}
queue.AddLast(head);
head = head.next;
}
ListNode dummy = new ListNode(0);
ListNode current = dummy;
while (queue.Count > 0){
current.next = queue.First.Value;
queue.RemoveFirst();
current = current.next;
}
return dummy.next;
}
}