diff --git a/rust/linked-lists/0002-add-two-numbers.rs b/rust/linked-lists/0002-add-two-numbers.rs new file mode 100644 index 0000000..b00dc78 --- /dev/null +++ b/rust/linked-lists/0002-add-two-numbers.rs @@ -0,0 +1,45 @@ +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn add_two_numbers(l1: Option>, l2: Option>) -> Option> { + fn add_lists(l1: Option>, l2: Option>, carry: i32) -> Option> { + if l1.is_none() && l2.is_none() && carry == 0 { + return None; + } + + let mut val = carry; + val += l1.as_ref().map(|l| l.val).unwrap_or(0); + val += l2.as_ref().map(|l| l.val).unwrap_or(0); + + let mut result = ListNode::new(val%10); + + if l1.is_some() || l2.is_some() { + let nxt = add_lists( + l1.and_then(|l| l.next), + l2.and_then(|l| l.next), + if val > 9 {1} else {0} + ); + + result.next = nxt; + } + + Some(Box::new(result)) + } + + add_lists(l1, l2, 0) + } +} \ No newline at end of file diff --git a/rust/linked-lists/0141-linked-list-cycle.rs b/rust/linked-lists/0141-linked-list-cycle.rs new file mode 100644 index 0000000..10c2dc2 --- /dev/null +++ b/rust/linked-lists/0141-linked-list-cycle.rs @@ -0,0 +1,15 @@ +from structures import ListNode + +def hasCycle(head: ListNode) -> bool: + fast = head + slow = head + + if head: + while fast.next and fast.next.next: + fast = fast.next.next + slow = slow.next + + if fast == slow: + return True + + return False \ No newline at end of file diff --git a/rust/linked-lists/0160-intersection-of-two-linked-lists.rs b/rust/linked-lists/0160-intersection-of-two-linked-lists.rs new file mode 100644 index 0000000..d733b19 --- /dev/null +++ b/rust/linked-lists/0160-intersection-of-two-linked-lists.rs @@ -0,0 +1,99 @@ +use std::cell::RefCell; +use std::rc::Rc; +use std::ops::Sub; + + +// Definition for a binary tree node. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ListNode { + pub val: i32, + pub next: Option>> +} + +impl ListNode { + #[inline] + pub fn new(val: i32) -> Self { + ListNode { + val, + next: None, + } + } +} + +type Node = Option>>; + +pub struct Solution(); +impl Solution { + pub fn get_intersection_node(head_a: Node, head_b: Node) -> Node { + let (mut len_a, mut len_b) = (0i32, 0i32); + + let mut cur_a = head_a.clone(); + while cur_a.is_some() { + cur_a = cur_a.unwrap().borrow().next.clone(); + len_a += 1; + } + + let mut cur_b = head_b.clone(); + while cur_b.is_some() { + cur_b = cur_b.unwrap().borrow().next.clone(); + len_b += 1; + } + + let (mut cur_a, mut cur_b) = (head_a.clone(), head_b.clone()); + let diff = len_a.sub(len_b).abs(); + if len_a < len_b { + for _ in 0..diff { + cur_b = cur_b.unwrap().borrow().next.clone(); + } + } + + if len_a > len_b { + for _ in 0..diff { + cur_a = cur_a.unwrap().borrow().next.clone(); + } + } + + while cur_a.is_some() && cur_b.is_some() { + if Rc::ptr_eq(&cur_a.as_ref().unwrap(), &cur_b.as_ref().unwrap()) { + return Some(cur_a.unwrap().clone()); + } + + cur_a = cur_a.unwrap().borrow().next.clone(); + cur_b = cur_b.unwrap().borrow().next.clone(); + } + + None + } +} + +fn main() { + let mut head_a = ListNode::new(4); + let mut a_1 = ListNode::new(1); + + let mut head_b = ListNode::new(5); + let mut b_1 = ListNode::new(6); + let mut b_2 = ListNode::new(1); + + let mut node_8 = ListNode::new(8); + let mut node_4 = ListNode::new(4); + let mut node_5 = ListNode::new(5); + + node_4.next = Some(Rc::new(RefCell::new(node_5))); + node_8.next = Some(Rc::new(RefCell::new(node_4))); + + let node_8_ref = Rc::new(RefCell::new(node_8)); + + a_1.next = Some(node_8_ref.clone()); + b_2.next = Some(node_8_ref.clone()); + + head_a.next = Some(Rc::new(RefCell::new(a_1))); + b_1.next = Some(Rc::new(RefCell::new(b_2))); + head_b.next = Some(Rc::new(RefCell::new(b_1))); + + let mut head_a = Some(Rc::new(RefCell::new(head_a))); + let mut head_b = Some(Rc::new(RefCell::new(head_b))); + + println!("A: {head_a:?}"); + println!("B: {head_b:?}"); + println!("intersection: {:?}", Solution::get_intersection_node(head_a, head_b)); +} \ No newline at end of file diff --git a/rust/linked-lists/0206-reverse-linked-list.rs b/rust/linked-lists/0206-reverse-linked-list.rs new file mode 100644 index 0000000..4b51bcb --- /dev/null +++ b/rust/linked-lists/0206-reverse-linked-list.rs @@ -0,0 +1,29 @@ +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn reverse_list(mut head: Option>) -> Option> { + let (mut prev, mut cur) = (None, head); + while let Some(mut cur_node) = cur { + let nx = cur_node.next.take(); + cur_node.next = prev; + prev = Some(cur_node); + cur = nx + } + + prev + } +} \ No newline at end of file diff --git a/rust/linked-lists/0234-palindrome-linked-list.rs b/rust/linked-lists/0234-palindrome-linked-list.rs new file mode 100644 index 0000000..8f86dfe --- /dev/null +++ b/rust/linked-lists/0234-palindrome-linked-list.rs @@ -0,0 +1,41 @@ +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn is_palindrome(head: Option>) -> bool { + let mut stack = vec![]; + let (mut slow, mut fast) = (head.as_ref(), head.as_ref()); + while fast.is_some() && fast.unwrap().next.is_some() { + stack.push(slow.unwrap().val); + slow = slow.unwrap().next.as_ref(); + fast = fast.unwrap().next.as_ref().unwrap().next.as_ref(); + } + + if fast.is_some() { + slow = slow.unwrap().next.as_ref(); + } + + while slow.is_some() { + let top = stack.pop().unwrap(); + let val = slow.unwrap().val; + if top != val {return false}; + + slow = slow.unwrap().next.as_ref(); + } + + true + } +} \ No newline at end of file diff --git a/rust/linked-lists/0328-odd-even-linked-list.rs b/rust/linked-lists/0328-odd-even-linked-list.rs new file mode 100644 index 0000000..25ff255 --- /dev/null +++ b/rust/linked-lists/0328-odd-even-linked-list.rs @@ -0,0 +1,42 @@ +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn odd_even_list(mut head: Option>) -> Option> { + if head.is_none() {return head}; + + let mut even = head.as_mut().unwrap().next.take(); + + let mut cur_odd = head.as_mut(); + let mut cur_even = even.as_mut(); + loop { + let even_has_next = cur_even.as_ref().and_then(|n| n.next.as_ref()).is_some(); + if !even_has_next {break}; + + let mut next_odd = cur_even.as_mut().unwrap().next.take(); + let mut next_even = next_odd.as_mut().unwrap().next.take(); + + cur_odd.as_mut().unwrap().next = next_odd; + cur_even.as_mut().unwrap().next = next_even; + + cur_odd = cur_odd.unwrap().next.as_mut(); + cur_even = cur_even.unwrap().next.as_mut(); + } + cur_odd.as_mut().unwrap().next = even; + + head + } +} \ No newline at end of file diff --git a/rust/linked-lists/1721-swapping-nodes-in-a-linked-list.rs b/rust/linked-lists/1721-swapping-nodes-in-a-linked-list.rs new file mode 100644 index 0000000..0c76e73 --- /dev/null +++ b/rust/linked-lists/1721-swapping-nodes-in-a-linked-list.rs @@ -0,0 +1,29 @@ +from typing import ListNode + + +def swapNodes(head: ListNode, k: int) -> ListNode: + list_len = 0 + cur = head + + while cur: + cur = cur.next + list_len += 1 + + left, right = None, None + left_idx, right_idx = k - 1, list_len - k + + cur = head + cur_idx = 0 + + while cur: + if cur_idx == left_idx: + left = cur + if cur_idx == right_idx: + right = cur + + cur = cur.next + cur_idx += 1 + + left.val, right.val = right.val, left.val + + return head \ No newline at end of file