1use crate::ListNode;
35
36pub struct Solution;
37
38impl Solution {
39
40 pub fn delete_node(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
41 let mut dummy = Some(Box::new(ListNode{ next: head, val: 0 }));
42 let mut node = &mut dummy;
43 while node.is_some() && node.as_ref().unwrap().next.is_some() {
44 if node.as_ref().unwrap().next.as_ref().unwrap().val == val {
45 let n = node.as_mut().unwrap().next.take();
46 node.as_mut().unwrap().next = n.unwrap().next;
47 break;
48 }
49 node = &mut node.as_mut().unwrap().next;
50 }
51 dummy.unwrap().next
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58 use crate::list;
59
60 #[test]
61 fn test() {
62 let cases = vec![
63 (vec![4,1,9], (list![4,5,1,9], 5)),
64 (vec![4,5,9], (list![4,5,1,9], 1)),
65 (vec![], (list![], 1)),
66 (vec![4,1,9], (list![4,1,9], 3)),
67 ];
68
69 for (expect, (head, val)) in cases {
70 assert_eq!(expect, ListNode::into_vec(Solution::delete_node(head, val)));
71 }
72 }
73}