/
githubmirror
/
hello-algo
Обзор
Документация
Войти
/
githubmirror
/
hello-algo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ja/codes/rust/src/include/list_node.rs
57 строк
1 KB
Yudong Jin
Re-translate the Japanese version (#1871)
30 мар 2026, 02:30
Не верифицирован
30 мар 2026, 02:30
d7b2277
Код
Авторство
О чём код?
/* * File: list_node.rs * Created Time: 2023-03-05 * Author: codingonion (coderonion@gmail.com), rongyi (hiarongyi@gmail.com) */ use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; #[derive(Debug)] pub struct ListNode<T> { pub val: T, pub next: Option<Rc<RefCell<ListNode<T>>>>, } impl<T> ListNode<T> { pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> { Rc::new(RefCell::new(ListNode { val, next: None })) } /* 配列をデシリアライズして連結リストに変換する */ pub fn arr_to_linked_list(array: &[T]) -> Option<Rc<RefCell<ListNode<T>>>> where T: Copy + Clone, { let mut head = None; // insert in reverse order for item in array.iter().rev() { let node = Rc::new(RefCell::new(ListNode { val: *item, next: head.take(), })); head = Some(node); } head } /* 連結リストをハッシュテーブルに変換 */ pub fn linked_list_to_hashmap( linked_list: Option<Rc<RefCell<ListNode<T>>>>, ) -> HashMap<T, Rc<RefCell<ListNode<T>>>> where T: std::hash::Hash + Eq + Copy + Clone, { let mut hashmap = HashMap::new(); let mut node = linked_list; while let Some(cur) = node { let borrow = cur.borrow(); hashmap.insert(borrow.val.clone(), cur.clone()); node = borrow.next.clone(); } hashmap } }