competitive_programming_lib/DataStructures/
linked_list.rs

1pub struct ListNode {
2    pub value: i32,
3    pub next: Option<Box<ListNode>>,
4}
5
6pub struct LinkedList {
7    pub head: Option<Box<ListNode>>,
8}
9
10impl LinkedList {
11    pub fn new() -> Self {
12        LinkedList { head: None }
13    }
14
15    pub fn push_front(&mut self, value: i32) {
16        let new_node = Box::new(ListNode {
17            value,
18            next: self.head.take(),
19        });
20        self.head = Some(new_node);
21    }
22
23    pub fn pop_front(&mut self) -> Option<i32> {
24        self.head.take().map(|node| {
25            self.head = node.next;
26            node.value
27        })
28    }
29
30    pub fn is_empty(&self) -> bool {
31        self.head.is_none()
32    }
33}