From 515962c059a70c0ac0fb5f0683c5c5718870e0f0 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 15 Jan 2024 12:00:43 +0800 Subject: [PATCH] 20240115 Finished --- src/problem/mod.rs | 4 +- ...2_remove_duplicates_from_sorted_list_ii.rs | 63 +++++++++++++++++++ .../p83_remove_duplicates_from_sorted_list.rs | 56 +++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/problem/p82_remove_duplicates_from_sorted_list_ii.rs create mode 100644 src/problem/p83_remove_duplicates_from_sorted_list.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 86ce119..a27f17e 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -15,4 +15,6 @@ mod p0004_median_of_two_sorted_arrays; mod p0743_network_delay_time; mod p0447_number_of_boomerangs; mod p2085_count_common_words_with_one_occurrence; -mod p2182_construct_string_with_repeat_limit; \ No newline at end of file +mod p2182_construct_string_with_repeat_limit; +mod p83_remove_duplicates_from_sorted_list; +mod p82_remove_duplicates_from_sorted_list_ii; \ No newline at end of file diff --git a/src/problem/p82_remove_duplicates_from_sorted_list_ii.rs b/src/problem/p82_remove_duplicates_from_sorted_list_ii.rs new file mode 100644 index 0000000..67045a0 --- /dev/null +++ b/src/problem/p82_remove_duplicates_from_sorted_list_ii.rs @@ -0,0 +1,63 @@ +/** + * [82] Remove Duplicates from Sorted List II + */ +pub struct Solution {} + +use crate::util::linked_list::{ListNode, to_list}; + +// submission codes start here + +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn delete_duplicates(head: Option>) -> Option> { + let mut head = head; + // 创建新链表的头结点 + let mut dummy = Box::new(ListNode::new(0)); + let mut tail = &mut dummy; + + let mut prev = None; + + while let Some(mut node) = head { + head = node.next.take(); + + let now = node.val; + + if head.as_ref().map_or(true, |next| next.val != now) + && prev.map_or(true, |pre| pre != now) { + + tail.next = Some(node); + tail = tail.next.as_mut()?; + } + + prev = Some(now); + } + + dummy.next + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_82() { + } +} diff --git a/src/problem/p83_remove_duplicates_from_sorted_list.rs b/src/problem/p83_remove_duplicates_from_sorted_list.rs new file mode 100644 index 0000000..723c7b5 --- /dev/null +++ b/src/problem/p83_remove_duplicates_from_sorted_list.rs @@ -0,0 +1,56 @@ +/** + * [83] Remove Duplicates from Sorted List + */ +pub struct Solution {} + +use crate::util::linked_list::{ListNode, to_list}; + +// submission codes start here + +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn delete_duplicates(head: Option>) -> Option> { + let mut head = head; + let mut now = head.as_mut(); + + while let Some(node) = now.take() { + while let Some(next) = node.next.as_mut() { + if node.val == next.val { + node.next = next.next.take(); + } else { + break + } + } + + now = node.next.as_mut(); + } + + head + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_83() { + } +}