Skip to content

Commit

Permalink
add: 删除排序链表中的重复元素 II
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 15, 2024
1 parent f09e044 commit c94fa7c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 链表

- [删除排序链表中的重复元素 II](src/list/remove_duplicates_from_sorted_list_ii.rs) [链表, 双指针]

- LeetCode 82. 删除排序链表中的重复元素 II <https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii>

- [在链表中插入最大公约数](src/list/insert_greatest_common_divisors_in_linked_list.rs) [链表, 数学, 数论]

- LeetCode 2807. 在链表中插入最大公约数 <https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
44 changes: 44 additions & 0 deletions src/list/remove_duplicates_from_sorted_list_ii.rs
Original file line number Diff line number Diff line change
@@ -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<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
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
}
}
1 change: 1 addition & 0 deletions tests/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
21 changes: 21 additions & 0 deletions tests/list/remove_duplicates_from_sorted_list_ii_test.rs
Original file line number Diff line number Diff line change
@@ -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]);
}

0 comments on commit c94fa7c

Please sign in to comment.