Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20 gabrielvictorcf rust linked list solutions #22

Merged
merged 7 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions rust/linked-lists/0002-add-two-numbers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
fn add_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>, carry: i32) -> Option<Box<ListNode>> {
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)
}
}
15 changes: 15 additions & 0 deletions rust/linked-lists/0141-linked-list-cycle.rs
Original file line number Diff line number Diff line change
@@ -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
99 changes: 99 additions & 0 deletions rust/linked-lists/0160-intersection-of-two-linked-lists.rs
Original file line number Diff line number Diff line change
@@ -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<Rc<RefCell<ListNode>>>
}

impl ListNode {
#[inline]
pub fn new(val: i32) -> Self {
ListNode {
val,
next: None,
}
}
}

type Node = Option<Rc<RefCell<ListNode>>>;

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));
}
29 changes: 29 additions & 0 deletions rust/linked-lists/0206-reverse-linked-list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
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
}
}
41 changes: 41 additions & 0 deletions rust/linked-lists/0234-palindrome-linked-list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn is_palindrome(head: Option<Box<ListNode>>) -> 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
}
}
42 changes: 42 additions & 0 deletions rust/linked-lists/0328-odd-even-linked-list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn odd_even_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
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
}
}
29 changes: 29 additions & 0 deletions rust/linked-lists/1721-swapping-nodes-in-a-linked-list.rs
Original file line number Diff line number Diff line change
@@ -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