leetcode/
remove_duplicates_from_sorted_list.rs1use crate::ListNode;
24
25pub struct Solution;
26
27impl Solution {
28 pub fn delete_duplicates(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
29 let mut p = &mut head;
30 while let Some(node) = p {
31 if let Some(next) = node.next.as_mut() {
32 if node.val == next.val {
33 node.next = next.next.take();
34 }
35 }
36 p = &mut node.next;
37 }
38 head
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45 use crate::list;
46
47 #[test]
48 fn test() {
49 let cases = vec![
50 (vec![1,2], list![1,1,2]),
51 (vec![1,2,3], list![1,1,2,3,3,]),
52 (vec![], list![]),
53 (vec![1,2], list![1,2]),
54 ];
55 let t = |v| ListNode::into_vec(Solution::delete_duplicates(v));
56 for (expect, input) in cases {
57 assert_eq!(expect, t(input));
58 }
59 }
60}