diff --git a/.vscode/settings.json b/.vscode/settings.json index 8fb0a8e..d342c84 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -127,5 +127,6 @@ "editor.defaultFormatter": "rust-lang.rust-analyzer", "editor.formatOnSave": true }, - "rust-analyzer.runnables.command": "python3 ${workspaceFolder}/bin/cargo_wrapper.py" + "rust-analyzer.runnables.command": "python3 ${workspaceFolder}/bin/cargo_wrapper.py", + "commentTranslate.hover.enabled": false } diff --git a/README.md b/README.md index 289ec13..50f2192 100644 --- a/README.md +++ b/README.md @@ -519,6 +519,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8 ### 链表 +- [删除排序链表中的重复元素 II](src/list/remove_duplicates_from_sorted_list_ii.rs) [链表, 双指针] + + - LeetCode 82. 删除排序链表中的重复元素 II + - [在链表中插入最大公约数](src/list/insert_greatest_common_divisors_in_linked_list.rs) [链表, 数学, 数论] - LeetCode 2807. 在链表中插入最大公约数 diff --git a/images/list/remove_duplicates_from_sorted_list_ii.jpeg b/images/list/remove_duplicates_from_sorted_list_ii.jpeg new file mode 100644 index 0000000..19cb48b Binary files /dev/null and b/images/list/remove_duplicates_from_sorted_list_ii.jpeg differ diff --git a/src/list/mod.rs b/src/list/mod.rs index b6786a1..348786e 100644 --- a/src/list/mod.rs +++ b/src/list/mod.rs @@ -2,3 +2,4 @@ pub mod insert_greatest_common_divisors_in_linked_list; pub mod middle_of_the_linked_list; pub mod remove_nth_node_from_end_of_list; pub mod remove_zero_sum_consecutive_nodes_from_linked_list; +pub mod remove_duplicates_from_sorted_list_ii; diff --git a/src/list/remove_duplicates_from_sorted_list_ii.rs b/src/list/remove_duplicates_from_sorted_list_ii.rs new file mode 100644 index 0000000..0fdbee5 --- /dev/null +++ b/src/list/remove_duplicates_from_sorted_list_ii.rs @@ -0,0 +1,44 @@ +// 删除排序链表中的重复元素 II +// https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii +// INLINE ../../images/list/remove_duplicates_from_sorted_list_ii.jpeg + +use crate::libs::list_node::ListNode; + +pub struct Solution; + +// 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 dummy_head = Box::new(ListNode { val: 0, next: head }); + let mut cur = &mut dummy_head; + let mut deleted_val = None; + while cur.next.is_some() { + if Some(cur.next.as_ref().unwrap().val) == deleted_val + || cur.next.as_ref().unwrap().next.is_some() + && cur.next.as_ref().unwrap().val + == cur.next.as_ref().unwrap().next.as_ref().unwrap().val + { + deleted_val = Some(cur.next.as_ref().unwrap().val); + cur.next = cur.next.take().unwrap().next; + } else { + cur = cur.next.as_mut().unwrap(); + } + } + dummy_head.next + } +} diff --git a/tests/list/mod.rs b/tests/list/mod.rs index 69eddb1..586f45d 100644 --- a/tests/list/mod.rs +++ b/tests/list/mod.rs @@ -2,3 +2,4 @@ pub mod middle_of_the_linked_list_test; pub mod remove_nth_node_from_end_of_list_test; pub mod remove_zero_sum_consecutive_nodes_from_linked_list_test; pub mod insert_greatest_common_divisors_in_linked_list_test; +pub mod remove_duplicates_from_sorted_list_ii_test; diff --git a/tests/list/remove_duplicates_from_sorted_list_ii_test.rs b/tests/list/remove_duplicates_from_sorted_list_ii_test.rs new file mode 100644 index 0000000..1302163 --- /dev/null +++ b/tests/list/remove_duplicates_from_sorted_list_ii_test.rs @@ -0,0 +1,21 @@ +use rust_practice::{ + libs::list_node::{list_node_to_vec, vec_to_list_node}, + list::remove_duplicates_from_sorted_list_ii::Solution, +}; + +#[test] +fn remove_duplicates_from_sorted_list_ii_test() { + // 示例 1: + // 输入:head = [1,2,3,3,4,4,5] + // 输出:[1,2,5] + let head = vec![1, 2, 3, 3, 4, 4, 5]; + let res = Solution::delete_duplicates(vec_to_list_node(&head)); + assert_eq!(list_node_to_vec(res), vec![1, 2, 5]); + + // 示例 2: + // 输入:head = [1,1,1,2,3] + // 输出:[2,3] + let head = vec![1, 1, 1, 2, 3]; + let res = Solution::delete_duplicates(vec_to_list_node(&head)); + assert_eq!(list_node_to_vec(res), vec![2, 3]); +}