fn add_at_index(&mut self, index: i32, val: i32) {
        if index < 0 || index > self.len as i32 {
            eprintln!("invalid index :{}", index);
            return;
        }
        if index == 0 {
            self.add_at_head(val);
            return;
        } else if self.root.is_some() && index == self.len as i32 {
            self.add_at_tail(val);
            return;
        }
        let mut current_index = 0;
        let mut current = &mut self.root;
        while current.is_some() {
            if (current_index == index - 1) {
                // if let Some(cur) = current {
                //     if let Some(next) = cur.next.take().as_ref() {
                //         cur.next = Some(Box::new(ListNode {
                //             val,
                //             next: Some(Box::new(*next.clone())),
                //         }));
                //     }

                //     break;
                // }
                let next = current.as_mut().unwrap().next.take();

                let node = Some(Box::new(ListNode { val, next }));

                current.as_mut().unwrap().next = node;
                self.len += 1;
                break;
            }
            current = &mut current.as_deref_mut().unwrap().next;

            current_index += 1;
        }
    }
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
Add a node of value val before the index - th node in the linked list . If index equals to the length of linked list the node will be appended to the end of linked list . If index is greater than the length the node will not be inserted .
fn add_at_index(&mut self, index: i32, val: i32) {
        if index < 0 || index > self.len as i32 {
            eprintln!("invalid index :{}", index);
            return;
        }
        if index == 0 {
            self.add_at_head(val);
            return;
        } else if self.root.is_some() && index == self.len as i32 {
            self.add_at_tail(val);
            return;
        }
        let mut current_index = 0;
        let mut current = &mut self.root;
        while current.is_some() {
            if (current_index == index - 1) {
                // if let Some(cur) = current {
                //     if let Some(next) = cur.next.take().as_ref() {
                //         cur.next = Some(Box::new(ListNode {
                //             val,
                //             next: Some(Box::new(*next.clone())),
                //         }));
                //     }
                //     break;
                // }
                let next = current.as_mut().unwrap().next.take();
                let node = Some(Box::new(ListNode { val, next }));
                current.as_mut().unwrap().next = node;
                self.len += 1;
                break;
            }
            current = &mut current.as_deref_mut().unwrap().next;
            current_index += 1;
        }
    }
fn add_at_index ( & mut self , index : i32 , val : i32 ) { if index < 0 || index > self . len as i32 { eprintln ! ( " " index ) ; return ; } if index == 0 { self . add_at_head ( val ) ; return ; } else if self . root . is_some ( ) && index == self . len as i32 { self . add_at_tail ( val ) ; return ; } let mut current_index = 0 ; let mut current = & mut self . root ; while current . is_some ( ) { if ( current_index == index - 1 ) { // if let Some(cur) = current { //     if let Some(next) = cur.next.take().as_ref() { //         cur.next = Some(Box::new(ListNode { //             val, //             next: Some(Box::new(*next.clone())), //         })); //     } //     break; // } let next = current . as_mut ( ) . unwrap ( ) . next . take ( ) ; let node = Some ( Box :: new ( ListNode { val , next } ) ) ; current . as_mut ( ) . unwrap ( ) . next = node ; self . len += 1 ; break ; } current = & mut current . as_deref_mut ( ) . unwrap ( ) . next ; current_index += 1 ; } }
Add a node of value val before the index-th node in the linked list.
Add a node of value val before the index - th node in the linked list .
&mut self
index
i32
val
i32
&mut self
index
i32
val
i32