Small refactor for p2 and p21, started implementing p25

This commit is contained in:
Egor 2024-05-07 15:42:09 +03:00
parent 0819e90e80
commit e714a84a6d
3 changed files with 41 additions and 31 deletions

View file

@ -2,22 +2,17 @@ use super::linked_list::ListNode;
pub struct Solution;
impl Solution {
pub fn merge_two_lists(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
pub fn merge_two_lists(mut list1: Option<Box<ListNode>>, mut list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
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)));

View file

@ -3,7 +3,25 @@ use crate::linked_list::ListNode;
pub struct Solution;
impl Solution {
pub fn reverse_k_group(mut head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
unimplemented!()
todo!()
}
fn swap_adjacent(head: &mut Option<Box<ListNode>>) {
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<Box<ListNode>>, length: i32) -> &mut Option<Box<ListNode>> {
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!();
}
}

View file

@ -2,30 +2,27 @@ use crate::linked_list::ListNode;
pub struct Solution;
impl Solution {
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let (mut l1, mut l2) = (l1, l2);
let mut head = Box::new(ListNode::new(0));
pub fn add_two_numbers(mut l1: Option<Box<ListNode>>, mut l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
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;
}
}