-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |