-
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
6 changed files
with
100 additions
and
0 deletions.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions
64
src/list/insert_greatest_common_divisors_in_linked_list.rs
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,64 @@ | ||
// 在链表中插入最大公约数 | ||
// https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list | ||
// INLINE ../../images/list/insert_greatest_common_divisors_in_linked_list.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 { | ||
fn gcd(mut a: i32, mut b: i32) -> i32 { | ||
// 如果 a 或 b 是负数,那么取绝对值 | ||
a = a.abs(); | ||
b = b.abs(); | ||
// 如果 a 或 b 是 0,那么返回另一个数 | ||
if a == 0 { | ||
return b; | ||
} | ||
if b == 0 { | ||
return a; | ||
} | ||
while b != 0 { | ||
let tmp = b; | ||
b = a % b; | ||
a = tmp; | ||
} | ||
a | ||
} | ||
|
||
pub fn insert_greatest_common_divisors( | ||
mut head: Option<Box<ListNode>>, | ||
) -> Option<Box<ListNode>> { | ||
if head.is_none() { | ||
return None; | ||
} | ||
|
||
let mut cur = &mut head; | ||
while cur.as_ref().unwrap().next.is_some() { | ||
let x = cur.as_mut().unwrap(); | ||
let next = x.next.take(); | ||
x.next = Some(Box::new(ListNode { | ||
val: Self::gcd(x.val, next.as_ref().unwrap().val), | ||
next, | ||
})); | ||
cur = &mut cur.as_mut().unwrap().next.as_mut().unwrap().next; | ||
} | ||
head | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,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; |
30 changes: 30 additions & 0 deletions
30
tests/list/insert_greatest_common_divisors_in_linked_list_test.rs
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,30 @@ | ||
use rust_practice::libs::list_node::vec_to_list_node; | ||
use rust_practice::list::insert_greatest_common_divisors_in_linked_list::Solution; | ||
|
||
#[test] | ||
fn insert_greatest_common_divisors() { | ||
// 示例 1: | ||
// 输入:head = [18,6,10,3] | ||
// 输出:[18,6,6,2,10,1,3] | ||
// 解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。 | ||
// - 18 和 6 的最大公约数为 6 ,插入第一和第二个结点之间。 | ||
// - 6 和 10 的最大公约数为 2 ,插入第二和第三个结点之间。 | ||
// - 10 和 3 的最大公约数为 1 ,插入第三和第四个结点之间。 | ||
// 所有相邻结点之间都插入完毕,返回链表。 | ||
let arr = vec![18, 6, 10, 3]; | ||
let head = vec_to_list_node(&arr); | ||
let res = vec![18, 6, 6, 2, 10, 1, 3]; | ||
let res = vec_to_list_node(&res); | ||
assert_eq!(Solution::insert_greatest_common_divisors(head), res); | ||
|
||
// 示例 2: | ||
// 输入:head = [7] | ||
// 输出:[7] | ||
// 解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。 | ||
// 没有相邻结点,所以返回初始链表。 | ||
let arr = vec![7]; | ||
let head = vec_to_list_node(&arr); | ||
let res = vec![7]; | ||
let res = vec_to_list_node(&res); | ||
assert_eq!(Solution::insert_greatest_common_divisors(head), res); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,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; |