题解 | #【模板】链表# in Rust

【模板】链表

https://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f

use std::io::{self, *};

#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ListNode {
    pub val: i32,
    pub next: Option<Box<ListNode>>,
}

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

fn main() {
    let stdin = stdin();
    let mut input = stdin.lock();
    let mut buffer = String::new();

    let mut head = Some(Box::new(ListNode::new(-1)));

    input.read_line(&mut buffer).expect("Failed to read line!");
    let tokens: Vec<usize> = buffer.split_whitespace().map(|s| {
        s.trim().parse::<usize>().expect("Expected an integer")
    }).collect();
    let n = tokens[0];
    for _ in 0..n {
        buffer.clear();
        input.read_line(&mut buffer).expect("Failed to read query");
        let tokens: Vec<&str> = buffer.trim().split_whitespace().collect();

        let op= tokens.get(0).unwrap();
        match *op {
            "insert" => {
                let (node_val, insert_val) = if let (Some(x), Some(y)) = (tokens.get(1), tokens.get(2)) {
                    (x.parse::<i32>().unwrap(), y.parse::<i32>().unwrap())
                } else {
                    (0, 0)
                };
                let mut p = &mut head;
                while let Some(ref mut node) = p {
                    if let Some(next_node) = node.next.as_ref() {
                        if node_val == next_node.val {
                            let new_node = Box::new(ListNode::new(insert_val));
                            let next = node.next.take();
                            node.next = Some(new_node);
                            node.next.as_mut().unwrap().next = next;
                            break;
                        }
                    } else {
                        // 找到了结尾
                        let new_node = Box::new(ListNode::new(insert_val));
                        node.next = Some(new_node);
                        break;
                    }
                    p = &mut node.next;
                }
            }
            "delete" => {
                // delete x
                let val = if let Some(x) = tokens.get(1) {
                    x.parse::<i32>().unwrap()
                } else {
                    0
                };
                let mut p = &mut head;
                while let Some(ref mut node) = p {
                    if let Some(ref mut next_node) = node.next {
                        if val == next_node.val {
                            node.next = next_node.next.take();
                            break;
                        }
                    }
                    p = &mut node.next;
                }
            }
            _ => { }
        };
    }

    let mut p = &head.unwrap().next;
    match p {
        Some(_) => {
            while let Some(node) = p {
                print!("{} ", node.val);
                p = &node.next;
            }
        }
        None => {
            print!("NULL");
        }
    }
}

全部评论

相关推荐

拒绝无效加班的小师弟很中意你:求职意向没有,年龄、课程冗余信息可以删掉,需要提升项目经历。排版需要修改。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务