diff --git a/src/lib.rs b/src/lib.rs index 4215b19..0431c52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,3 +23,4 @@ pub mod p20_valid_parentheses; pub mod p21_merge_two_sorted_lists; pub mod p22_generate_parentheses; pub mod p23_merge_k_sorted_lists; +pub mod p24_swap_nodes_in_pairs; diff --git a/src/p19_remove_nth_node_from_end_of_list.rs b/src/p19_remove_nth_node_from_end_of_list.rs index 8357307..9d8ba5d 100644 --- a/src/p19_remove_nth_node_from_end_of_list.rs +++ b/src/p19_remove_nth_node_from_end_of_list.rs @@ -1,26 +1,24 @@ -use super::linked_list::ListNode; +use crate::linked_list::ListNode; pub struct Solution; impl Solution { pub fn remove_nth_from_end(head: Option>, n: i32) -> Option> { - let mut head = head.unwrap(); - let mut node_before_nth: &mut Box; + let mut dummy = Some(Box::new(ListNode { val: 0, next: head })); + let mut node_before_nth: &mut Option>; // unsafe is required to have immutable and mutable references at the same time - // in order to avoid cloning the list altogether - unsafe { node_before_nth = &mut *(&mut head as *mut Box); }; - let mut tail = &head; - for _ in 0..n-1 { - tail = tail.next.as_ref().unwrap(); + // in order to avoid cloning the list + unsafe { node_before_nth = &mut *(&mut dummy as *mut Option>); }; + let mut tail = &dummy.as_ref().unwrap().next; + for _ in 0..n { + tail = &tail.as_ref().unwrap().next; } - if tail.next.is_none() { return head.next; } - else { tail = tail.next.as_ref().unwrap(); } - while tail.next.is_some() { - tail = tail.next.as_ref().unwrap(); - node_before_nth = node_before_nth.next.as_mut().unwrap(); + while tail.is_some() { + tail = &tail.as_ref().unwrap().next; + node_before_nth = &mut node_before_nth.as_mut().unwrap().next; } - let node_to_remove = node_before_nth.next.as_mut().unwrap(); - node_before_nth.next = node_to_remove.next.take(); - return Some(head); + let node_to_remove = &mut node_before_nth.as_mut().unwrap().next; + node_before_nth.as_mut().unwrap().next = node_to_remove.as_mut().unwrap().next.take(); + return dummy.unwrap().next; } } diff --git a/src/p24_swap_nodes_in_pairs.rs b/src/p24_swap_nodes_in_pairs.rs new file mode 100644 index 0000000..2cdc6b3 --- /dev/null +++ b/src/p24_swap_nodes_in_pairs.rs @@ -0,0 +1,34 @@ +use crate::linked_list::ListNode; + +pub struct Solution; +impl Solution { + pub fn swap_pairs(mut head: Option>) -> Option> { + 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]); +} diff --git a/src/p2_add_two_numbers.rs b/src/p2_add_two_numbers.rs index ae6db69..ce656d7 100644 --- a/src/p2_add_two_numbers.rs +++ b/src/p2_add_two_numbers.rs @@ -1,4 +1,4 @@ -use super::linked_list::ListNode; +use crate::linked_list::ListNode; pub struct Solution; impl Solution {