203. Remove Linked List Elements Easy
1/**
2 * [203] Remove Linked List Elements
3 *
4 * Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
5 *
6 * Example 1:
7 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/06/removelinked-list.jpg" style="width: 500px; height: 142px;" />
8 * Input: head = [1,2,6,3,4,5,6], val = 6
9 * Output: [1,2,3,4,5]
10 *
11 * Example 2:
12 *
13 * Input: head = [], val = 1
14 * Output: []
15 *
16 * Example 3:
17 *
18 * Input: head = [7,7,7,7], val = 7
19 * Output: []
20 *
21 *
22 * Constraints:
23 *
24 * The number of nodes in the list is in the range [0, 10^4].
25 * 1 <= Node.val <= 50
26 * 0 <= val <= 50
27 *
28 */
29pub struct Solution {}
30use crate::util::linked_list::{ListNode, to_list};
31
32// problem: https://leetcode.com/problems/remove-linked-list-elements/
33// discuss: https://leetcode.com/problems/remove-linked-list-elements/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37// Definition for singly-linked list.
38// #[derive(PartialEq, Eq, Clone, Debug)]
39// pub struct ListNode {
40// pub val: i32,
41// pub next: Option<Box<ListNode>>
42// }
43//
44// impl ListNode {
45// #[inline]
46// fn new(val: i32) -> Self {
47// ListNode {
48// next: None,
49// val
50// }
51// }
52// }
53impl Solution {
54 pub fn remove_elements(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
55 Some(Box::new(ListNode::new(0)))
56 }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_203() {
67 }
68}
69
Back
© 2025 bowen.ge All Rights Reserved.