20240115 Finished

This commit is contained in:
jackfiled 2024-01-15 12:00:43 +08:00
parent d73d1ab43a
commit 515962c059
3 changed files with 122 additions and 1 deletions

View File

@ -16,3 +16,5 @@ mod p0743_network_delay_time;
mod p0447_number_of_boomerangs; mod p0447_number_of_boomerangs;
mod p2085_count_common_words_with_one_occurrence; mod p2085_count_common_words_with_one_occurrence;
mod p2182_construct_string_with_repeat_limit; mod p2182_construct_string_with_repeat_limit;
mod p83_remove_duplicates_from_sorted_list;
mod p82_remove_duplicates_from_sorted_list_ii;

View File

@ -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<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 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() {
}
}

View File

@ -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<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 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() {
}
}