Rust 刷leetcode 第二题

题目描述

力扣

// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
    pub val: i32,
    pub next: Option<Box<ListNode>>,
}



struct Solution {}

impl Solution {
    pub fn add_two_numbers(
        l1: Option<Box<ListNode>>,
        l2: Option<Box<ListNode>>,
    ) -> Option<Box<ListNode>> {
        let mut number1 = 0;
        let mut number2 = 0;
        if let Some(ref box_list_node) = l1 {
            number1 = to_number(box_list_node);
        }
        if let Some(ref box_list_node) = l2 {
            number2 = to_number(box_list_node);
        }

        println!("number1 = {}", number1);
        println!("number2 = {}", number2);

        println!("sum = {}", number1 + number2);

        let ret = num_to_list_node(number1 + number2);

        return ret;
    }
}

pub fn to_number(list_node: &ListNode) -> i32 {
    let mut ret: Vec<i32> = Vec::new();

    let mut t = list_node;
    loop {
        ret.push(t.val);
        match t.next {
            Some(ref temp) =>  {t = temp;}
            None => {
                break;
            }
        }
    }
    let mut sum = 0;
    let mut acc = 1;

    for i in ret {
        sum += i * acc;
        acc *= 10;
    }
    return sum;
}

pub fn num_to_list_node(number:  i32) -> Option<Box<ListNode>>{
    
    let mut list_node1 = ListNode::new(0);
    let mut ptr = &mut list_node1;

    let mut t = number;

    loop {
        if t > 9 {
            let temp = t % 10;
            print!("  = {}", temp);

            ptr.next = Some(Box::new(ListNode::new(temp)));

            if let Some(ref mut box_node) = ptr.next {
                ptr = box_node;
            }

            t = t / 10;

        }else {
            print!("  = {}", t);
            ptr.next = Some(Box::new(ListNode::new(t)));

            break;
        }
    }
    println!();
    println!("ret = {}", to_number(&list_node1));

    return list_node1.next;

}

impl ListNode {
    #[inline]
    pub fn new(val: i32) -> Self {
        ListNode { next: None, val }
    }

    // 想修改节点,必须返回可变借用
    pub fn get_last_mut(&mut self) -> &mut Self {
        match self.next {
            Some(ref mut temp) => {
                return temp.get_last_mut();
            }
            None => {
                return self;
            }
        }

    }


    // 追加节点
    pub fn append(&mut self, val: i32) {
        let _node = ListNode::new(val);
        self.get_last_mut().next = Some(Box::new(_node));
    }
}


// [9]
// [1,9,9,9,9,9,9,9,9,9]
fn main() {
    let mut list_node = ListNode::new(9);

    let mut list_node2 = ListNode::new(1);
    list_node2.append(9);
    list_node2.append(9);
    list_node2.append(9);
    list_node2.append(9);
    list_node2.append(9);
    list_node2.append(9);
    list_node2.append(9);
    list_node2.append(9);
    list_node2.append(9);

    Solution::add_two_numbers(Some(Box::new(list_node)), Some(Box::new(list_node2)));

}

这个思路很简单,先将链表转换为i32,加起来之后再将i32转换成链表,不过这个思路是有问题的,会有溢出的问题。需用原始的链表来解决。

上一篇:Rust中的String类型


下一篇:IP数据报头部