2487. Remove Nodes From Linked List Medium

@problem@discussion
#Linked List#Stack#Recursion#Monotonic Stack



1/**
2 * [2487] Remove Nodes From Linked List
3 *
4 * You are given the head of a linked list.
5 * Remove every node which has a node with a strictly greater value anywhere to the right side of it.
6 * Return the head of the modified linked list.
7 *  
8 * <strong class="example">Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2022/10/02/drawio.png" style="width: 631px; height: 51px;" />
10 * Input: head = [5,2,13,3,8]
11 * Output: [13,8]
12 * Explanation: The nodes that should be removed are 5, 2 and 3.
13 * - Node 13 is to the right of node 5.
14 * - Node 13 is to the right of node 2.
15 * - Node 8 is to the right of node 3.
16 * 
17 * <strong class="example">Example 2:
18 * 
19 * Input: head = [1,1,1,1]
20 * Output: [1,1,1,1]
21 * Explanation: Every node has value 1, so no nodes are removed.
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	The number of the nodes in the given list is in the range [1, 10^5].
27 * 	1 <= Node.val <= 10^5
28 * 
29 */
30pub struct Solution {}
31use crate::util::linked_list::{ListNode, to_list};
32
33// problem: https://leetcode.com/problems/remove-nodes-from-linked-list/
34// discuss: https://leetcode.com/problems/remove-nodes-from-linked-list/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38// Definition for singly-linked list.
39// #[derive(PartialEq, Eq, Clone, Debug)]
40// pub struct ListNode {
41//   pub val: i32,
42//   pub next: Option<Box<ListNode>>
43// }
44// 
45// impl ListNode {
46//   #[inline]
47//   fn new(val: i32) -> Self {
48//     ListNode {
49//       next: None,
50//       val
51//     }
52//   }
53// }
54impl Solution {
55    pub fn remove_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
56        Some(Box::new(ListNode::new(0)))
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_2487() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.