From e714a84a6d885754a4815155be4332c4fab4cba8 Mon Sep 17 00:00:00 2001 From: erius Date: Tue, 7 May 2024 15:42:09 +0300 Subject: [PATCH] Small refactor for p2 and p21, started implementing p25 --- src/p21_merge_two_sorted_lists.rs | 17 +++++--------- src/p25_reverse_nodes_in_k_group.rs | 20 ++++++++++++++++- src/p2_add_two_numbers.rs | 35 +++++++++++++---------------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/p21_merge_two_sorted_lists.rs b/src/p21_merge_two_sorted_lists.rs index 297f9b7..f945828 100644 --- a/src/p21_merge_two_sorted_lists.rs +++ b/src/p21_merge_two_sorted_lists.rs @@ -2,22 +2,17 @@ use super::linked_list::ListNode; pub struct Solution; impl Solution { - pub fn merge_two_lists(list1: Option>, list2: Option>) -> Option> { + pub fn merge_two_lists(mut list1: Option>, mut list2: Option>) -> Option> { let mut head = None; let mut tail = &mut head; - let (mut l1, mut l2) = (&list1, &list2); - while l1.is_some() || l2.is_some() { - let val1 = if l1.is_some() { - l1.as_ref().unwrap().val - } else { i32::MAX }; - let val2 = if l2.is_some() { - l2.as_ref().unwrap().val - } else { i32::MAX }; + while list1.is_some() || list2.is_some() { + let val1 = list1.as_ref().map_or(i32::MAX, |node| node.val); + let val2 = list2.as_ref().map_or(i32::MAX, |node| node.val); let num = if val1 < val2 { - l1 = &l1.as_ref().unwrap().next; + list1 = list1.unwrap().next; val1 } else { - l2 = &l2.as_ref().unwrap().next; + list2 = list2.unwrap().next; val2 }; *tail = Some(Box::new(ListNode::new(num))); diff --git a/src/p25_reverse_nodes_in_k_group.rs b/src/p25_reverse_nodes_in_k_group.rs index b3b9aa3..4d0b95d 100644 --- a/src/p25_reverse_nodes_in_k_group.rs +++ b/src/p25_reverse_nodes_in_k_group.rs @@ -3,7 +3,25 @@ use crate::linked_list::ListNode; pub struct Solution; impl Solution { pub fn reverse_k_group(mut head: Option>, k: i32) -> Option> { - unimplemented!() + todo!() + } + + fn swap_adjacent(head: &mut Option>) { + let next = &mut head.as_mut().unwrap().next; + let mut next_node = next.take(); + head.as_mut().unwrap().next = next_node.as_mut().unwrap().next.take(); + next_node.as_mut().unwrap().next = head.take(); + head.replace(next_node.unwrap()); + } + + fn reverse_list(mut tail: &mut Option>, length: i32) -> &mut Option> { + let head = &mut *tail; + for _ in 0..length { + if tail.as_mut().unwrap().next.is_none() { break; } + Self::swap_adjacent(tail); + tail = &mut tail.as_mut().unwrap().next; + } + todo!(); } } diff --git a/src/p2_add_two_numbers.rs b/src/p2_add_two_numbers.rs index 7578b2c..b78d162 100644 --- a/src/p2_add_two_numbers.rs +++ b/src/p2_add_two_numbers.rs @@ -2,31 +2,28 @@ use crate::linked_list::ListNode; pub struct Solution; impl Solution { - pub fn add_two_numbers(l1: Option>, l2: Option>) -> Option> { - let (mut l1, mut l2) = (l1, l2); - let mut head = Box::new(ListNode::new(0)); + pub fn add_two_numbers(mut l1: Option>, mut l2: Option>) -> Option> { + let mut head = None; let mut tail = &mut head; - loop { - let mut sum = tail.val; - if let Some(node) = &l1 { + let mut carry = 0; + while l1.is_some() || l2.is_some() { + let mut sum = carry; + if let Some(mut node) = l1.take() { sum += node.val; - l1 = node.next.clone(); + l1 = node.next.take() } - if let Some(node) = &l2 { + if let Some(mut node) = l2.take() { sum += node.val; - l2 = node.next.clone(); + l2 = node.next.take() } - let next_node = Box::new(ListNode::new(sum / 10)); - tail.val = sum % 10; - if l1.is_none() && l2.is_none() { - if next_node.val > 0 { tail.next = Some(next_node); } - break; - } - tail.next = Some(next_node); - tail = tail.next.as_mut().unwrap(); + let new_node = Box::new(ListNode::new(sum % 10)); + carry = sum / 10; + *tail = Some(new_node); + tail = &mut tail.as_mut().unwrap().next; } - return Some(head); - } + if carry > 0 { *tail = Some(Box::new(ListNode::new(carry))); } + return head; + } } #[cfg(test)]