Implemented p24, changed linked_list impotr to always use crate in its path instead of super, modified p19

This commit is contained in:
Egor 2024-05-04 04:15:42 +03:00
parent b7efae53d1
commit e4dbd39192
4 changed files with 50 additions and 17 deletions

View file

@ -23,3 +23,4 @@ pub mod p20_valid_parentheses;
pub mod p21_merge_two_sorted_lists; pub mod p21_merge_two_sorted_lists;
pub mod p22_generate_parentheses; pub mod p22_generate_parentheses;
pub mod p23_merge_k_sorted_lists; pub mod p23_merge_k_sorted_lists;
pub mod p24_swap_nodes_in_pairs;

View file

@ -1,26 +1,24 @@
use super::linked_list::ListNode; use crate::linked_list::ListNode;
pub struct Solution; pub struct Solution;
impl Solution { impl Solution {
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> { pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
let mut head = head.unwrap(); let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
let mut node_before_nth: &mut Box<ListNode>; let mut node_before_nth: &mut Option<Box<ListNode>>;
// unsafe is required to have immutable and mutable references at the same time // unsafe is required to have immutable and mutable references at the same time
// in order to avoid cloning the list altogether // in order to avoid cloning the list
unsafe { node_before_nth = &mut *(&mut head as *mut Box<ListNode>); }; unsafe { node_before_nth = &mut *(&mut dummy as *mut Option<Box<ListNode>>); };
let mut tail = &head; let mut tail = &dummy.as_ref().unwrap().next;
for _ in 0..n-1 { for _ in 0..n {
tail = tail.next.as_ref().unwrap(); tail = &tail.as_ref().unwrap().next;
} }
if tail.next.is_none() { return head.next; } while tail.is_some() {
else { tail = tail.next.as_ref().unwrap(); } tail = &tail.as_ref().unwrap().next;
while tail.next.is_some() { node_before_nth = &mut node_before_nth.as_mut().unwrap().next;
tail = tail.next.as_ref().unwrap();
node_before_nth = node_before_nth.next.as_mut().unwrap();
} }
let node_to_remove = node_before_nth.next.as_mut().unwrap(); let node_to_remove = &mut node_before_nth.as_mut().unwrap().next;
node_before_nth.next = node_to_remove.next.take(); node_before_nth.as_mut().unwrap().next = node_to_remove.as_mut().unwrap().next.take();
return Some(head); return dummy.unwrap().next;
} }
} }

View file

@ -0,0 +1,34 @@
use crate::linked_list::ListNode;
pub struct Solution;
impl Solution {
pub fn swap_pairs(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() { return None; }
let mut tail = &mut head;
while tail.is_some() {
let next = &mut tail.as_mut().unwrap().next;
if next.is_none() { break; }
let mut next_node = next.take();
tail.as_mut().unwrap().next = next_node.as_mut().unwrap().next.take();
next_node.as_mut().unwrap().next = tail.take();
tail.replace(next_node.unwrap());
tail = &mut tail.as_mut().unwrap().next.as_mut().unwrap().next;
}
return head;
}
}
#[test]
fn test1() {
assert_eq!(Solution::swap_pairs(list![1,2,3,4]), list![2,1,4,3]);
}
#[test]
fn test2() {
assert_eq!(Solution::swap_pairs(list![]), list![]);
}
#[test]
fn test3() {
assert_eq!(Solution::swap_pairs(list![1]), list![1]);
}

View file

@ -1,4 +1,4 @@
use super::linked_list::ListNode; use crate::linked_list::ListNode;
pub struct Solution; pub struct Solution;
impl Solution { impl Solution {