@ -1,12 +1,12 @@
/*
/*
* File : list_node . rs
* File : list_node . rs
* Created Time : 2023 - 03 - 05
* Created Time : 2023 - 03 - 05
* Author : codingonion ( coderonion @ gmail . com )
* Author : codingonion ( coderonion @ gmail . com ) , rongyi ( hiarongyi @ gmail . com )
* /
* /
use std ::rc ::Rc ;
use std ::cell ::RefCell ;
use std ::cell ::RefCell ;
use std ::collections ::HashMap ;
use std ::collections ::HashMap ;
use std ::rc ::Rc ;
#[ derive(Debug) ]
#[ derive(Debug) ]
pub struct ListNode < T > {
pub struct ListNode < T > {
@ -16,10 +16,7 @@ pub struct ListNode<T> {
impl < T > ListNode < T > {
impl < T > ListNode < T > {
pub fn new ( val : T ) -> Rc < RefCell < ListNode < T > > > {
pub fn new ( val : T ) -> Rc < RefCell < ListNode < T > > > {
Rc ::new ( RefCell ::new ( ListNode {
Rc ::new ( RefCell ::new ( ListNode { val , next : None } ) )
val ,
next : None ,
} ) )
}
}
/* Generate a linked list with an array */
/* Generate a linked list with an array */
@ -28,13 +25,12 @@ impl<T> ListNode<T> {
T : Copy + Clone ,
T : Copy + Clone ,
{
{
let mut head = None ;
let mut head = None ;
let mut prev = None ;
// insert in reverse order
for item in array . iter ( ) . rev ( ) {
for item in array . iter ( ) . rev ( ) {
let node = Rc ::new ( RefCell ::new ( ListNode {
let node = Rc ::new ( RefCell ::new ( ListNode {
val : * item ,
val : * item ,
next : prev. tak e( ) ,
next : head. clon e( ) ,
} ) ) ;
} ) ) ;
prev = Some ( node . clone ( ) ) ;
head = Some ( node ) ;
head = Some ( node ) ;
}
}
head
head
@ -45,7 +41,7 @@ impl<T> ListNode<T> {
linked_list : Option < Rc < RefCell < ListNode < T > > > > ,
linked_list : Option < Rc < RefCell < ListNode < T > > > > ,
) -> HashMap < T , Rc < RefCell < ListNode < T > > > >
) -> HashMap < T , Rc < RefCell < ListNode < T > > > >
where
where
T : std ::hash ::Hash + Eq + Copy + Clone
T : std ::hash ::Hash + Eq + Copy + Clone ,
{
{
let mut hashmap = HashMap ::new ( ) ;
let mut hashmap = HashMap ::new ( ) ;
if let Some ( node ) = linked_list {
if let Some ( node ) = linked_list {