Skip to content

Commit

Permalink
setup link list
Browse files Browse the repository at this point in the history
  • Loading branch information
SKTT1Ryze committed Oct 11, 2023
1 parent 9079465 commit 2e974cc
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 5 deletions.
92 changes: 87 additions & 5 deletions leetcode/src/list.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,95 @@
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
pub struct ListNode<T: PartialEq + Eq> {
pub val: T,
pub next: Option<Box<ListNode<T>>>,
}

impl ListNode {
impl<T: PartialEq + Eq> ListNode<T> {
#[inline]
fn new(val: i32) -> Self {
fn new(val: T) -> Self {
ListNode { next: None, val }
}

pub fn create_list<const N: usize>(v: [T; N]) -> Option<Box<Self>> {
let mut head = None;

for item in v.into_iter().rev() {
head = Some(Box::new(ListNode {
val: item,
next: head,
}));
}

head
}
}

pub fn assert_eq_list<T: PartialEq + Eq>(
l1: &Option<Box<ListNode<T>>>,
l2: &Option<Box<ListNode<T>>>,
) -> bool {
let mut p1 = l1;
let mut p2 = l2;
while let (Some(node1), Some(node2)) = (p1, p2) {
if node1.val != node2.val {
return false;
}
p1 = &node1.next;
p2 = &node2.next;
}

p1.is_none() && p2.is_none()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_create_list() {
let v = [1, 3, 2];
let list = ListNode::create_list(v.clone());

let mut head = &list;
let mut v = Vec::from(v);
while let Some(node) = head {
let item = v.remove(0);
assert_eq!(item, node.val);
head = &node.next;
}
}

#[test]
fn test_assert_eq_list() {
let l1 = Some(Box::new(ListNode {
val: 0,
next: Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode { val: 2, next: None })),
})),
}));
let l2 = Some(Box::new(ListNode {
val: 0,
next: Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode { val: 2, next: None })),
})),
}));
let l3 = Some(Box::new(ListNode {
val: 0,
next: Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode { val: 3, next: None })),
})),
}));
let l4 = Some(Box::new(ListNode {
val: 0,
next: Some(Box::new(ListNode { val: 1, next: None })),
}));

assert!(assert_eq_list(&l1, &l2));
assert!(!assert_eq_list(&l1, &l3));
assert!(!assert_eq_list(&l1, &l4));
}
}
1 change: 1 addition & 0 deletions leetcode/src/solutions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;

pub mod add_two_numbers;
pub mod two_sum;

pub trait Solution: Send + Sync {
Expand Down
54 changes: 54 additions & 0 deletions leetcode/src/solutions/add_two_numbers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use super::Solution;
use crate::list::{assert_eq_list, ListNode};

pub struct AddTwoNumbers;

impl Solution for AddTwoNumbers {
fn name(&self) -> String {
"Add Two Numbers Solution".into()
}
fn problem_id(&self) -> usize {
2
}
fn test(&self) -> anyhow::Result<()> {
let testcases = [
(
ListNode::create_list([2, 4, 3]),
ListNode::create_list([5, 6, 4]),
ListNode::create_list([7, 0, 8]),
),
(
ListNode::create_list([0]),
ListNode::create_list([0]),
ListNode::create_list([0]),
),
(
ListNode::create_list([9, 9, 9, 9, 9, 9, 9]),
ListNode::create_list([9, 9, 9, 9]),
ListNode::create_list([8, 9, 9, 9, 0, 0, 0, 1]),
),
];

for (l1, l2, expect) in testcases {
let output = Self::add_two_numbers(l1, l2);

if !assert_eq_list(&expect, &output) {
// TODO: print link list
anyhow::bail!("test failed in AddTwoNumbers solution");
}
}
Ok(())
}
fn benchmark(&self) -> anyhow::Result<usize> {
anyhow::bail!("TODO");
}
}

impl AddTwoNumbers {
pub fn add_two_numbers(
l1: Option<Box<ListNode<i32>>>,
l2: Option<Box<ListNode<i32>>>,
) -> Option<Box<ListNode<i32>>> {
todo!()
}
}

0 comments on commit 2e974cc

Please sign in to comment.