pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
Expand description
§Description
The struct works by having a value and an optional pointer to a next ListNode. When writing the nodes you need to start from the last one and make your way down to the first one.
The struct signature is entirely 1:1 with LeetCode with additional optional features like serde.
§Example
use leetcode_trees_rs::utils::ListNode;
use std::boxed::Box;
use std::option::Option;
// Last node
let node_3 = ListNode::new(7);
// Second node
let node_2 = ListNode{
val: 6,
next: Some(Box::new(node_3)),
};
// First node
let node_1 = ListNode {
val: 5,
next: Some(Box::new(node_2)),
};
Result: 5 -> 6 -> 7 -> None
§Big O
Peeking/modifying the first element -> O(1)
Peeking/modifying the last element -> O(n)
Removing the first element -> O(1)
Removing any next element -> O(n)
Adding a new element at the end -> O(n)
Adding a new element at the start -> O(1)
Searching -> O(n)
Fields§
§val: i32
This uses normal i32 values that can be serialized and deserialized using serde if wanted.
next: Option<Box<ListNode>>
An optional box for a next ListNode.
Implementations§
Trait Implementations§
impl Eq for ListNode
impl StructuralPartialEq for ListNode
Auto Trait Implementations§
impl Freeze for ListNode
impl RefUnwindSafe for ListNode
impl Send for ListNode
impl Sync for ListNode
impl Unpin for ListNode
impl UnwindSafe for ListNode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more