fn add_at_tail(&mut self, val: i32) {
        if self.root.is_none() {
            self.root = Some(Box::new(ListNode::new(val)));
            self.len = 1;
        } else {
            let mut current = &mut self.root;
            while current.is_some() {
                let next = &current.as_ref().unwrap().next;
                //判断当前元素current是否为最后一个元素
                if next.is_none() {
                    if let Some(last) = current.as_deref_mut() {
                        last.next = Some(Box::new(ListNode::new(val)));
                        self.len += 1;
                        break;
                    }
                }

                current = &mut current.as_deref_mut().unwrap().next;
            }
        }
    }
/** Append a node of value val to the last element of the linked list. */
Append a node of value val to the last element of the linked list.
Append a node of value val to the last element of the linked list .
fn add_at_tail(&mut self, val: i32) {
        if self.root.is_none() {
            self.root = Some(Box::new(ListNode::new(val)));
            self.len = 1;
        } else {
            let mut current = &mut self.root;
            while current.is_some() {
                let next = &current.as_ref().unwrap().next;
                //判断当前元素current是否为最后一个元素
                if next.is_none() {
                    if let Some(last) = current.as_deref_mut() {
                        last.next = Some(Box::new(ListNode::new(val)));
                        self.len += 1;
                        break;
                    }
                }
                current = &mut current.as_deref_mut().unwrap().next;
            }
        }
    }
fn add_at_tail ( & mut self , val : i32 ) { if self . root . is_none ( ) { self . root = Some ( Box :: new ( ListNode :: new ( val ) ) ) ; self . len = 1 ; } else { let mut current = & mut self . root ; while current . is_some ( ) { let next = & current . as_ref ( ) . unwrap ( ) . next ; //判断当前元素current是否为最后一个元素 if next . is_none ( ) { if let Some ( last ) = current . as_deref_mut ( ) { last . next = Some ( Box :: new ( ListNode :: new ( val ) ) ) ; self . len += 1 ; break ; } } current = & mut current . as_deref_mut ( ) . unwrap ( ) . next ; } } }
Append a node of value val to the last element of the linked list.
Append a node of value val to the last element of the linked list .
&mut self
val
i32
&mut self
val
i32