-
Notifications
You must be signed in to change notification settings - Fork 150
Add Rust implementations for Linked Lists patterns #98
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
Open
Peponks9
wants to merge
1
commit into
ByteByteGoHq:main
Choose a base branch
from
Peponks9:feature/add-rust-linked-lists
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#[derive(Debug)] | ||
struct MultiLevelListNode { | ||
val: i32, | ||
next: Option<Box<MultiLevelListNode>>, | ||
child: Option<Box<MultiLevelListNode>>, | ||
} | ||
|
||
impl MultiLevelListNode { | ||
fn new(val: i32) -> Self { | ||
MultiLevelListNode { | ||
val, | ||
next: None, | ||
child: None, | ||
} | ||
} | ||
} | ||
|
||
fn flatten_multi_level_list( | ||
head: Option<Box<MultiLevelListNode>>, | ||
) -> Option<Box<MultiLevelListNode>> { | ||
if head.is_none() { | ||
return None; | ||
} | ||
let mut head = head; | ||
let mut tail = head.as_mut().unwrap(); | ||
// Find the tail of the linked list at the first level. | ||
while let Some(ref next) = tail.next { | ||
tail = tail.next.as_ref().unwrap(); | ||
} | ||
let mut curr = head.as_mut().unwrap(); | ||
// Process each node at the current level. If a node has a child linked list, | ||
// append it to the tail and then update the tail to the end of the extended | ||
// linked list. Continue until all nodes at the current level are processed. | ||
while let Some(ref mut current) = curr.next { | ||
if let Some(child) = current.child.take() { | ||
tail.next = Some(child); | ||
// Update tail to the end of the child list | ||
while let Some(ref next) = tail.next { | ||
tail = tail.next.as_ref().unwrap(); | ||
} | ||
} | ||
curr = current; | ||
} | ||
head | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
#[derive(Debug)] | ||
struct ListNode { | ||
val: i32, | ||
next: Option<Rc<RefCell<ListNode>>>, | ||
} | ||
|
||
impl ListNode { | ||
fn new(val: i32) -> Rc<RefCell<Self>> { | ||
Rc::new(RefCell::new(ListNode { val, next: None })) | ||
} | ||
} | ||
|
||
fn linked_list_intersection( | ||
head_a: Option<Rc<RefCell<ListNode>>>, | ||
head_b: Option<Rc<RefCell<ListNode>>>, | ||
) -> Option<Rc<RefCell<ListNode>>> { | ||
let mut ptr_a = head_a.clone(); | ||
let mut ptr_b = head_b.clone(); | ||
// Traverse through list A with 'ptr_a' and list B with 'ptr_b' | ||
// until they meet. | ||
while ptr_a != ptr_b { | ||
// Traverse list A -> list B by first traversing 'ptr_a' and | ||
// then, upon reaching the end of list A, continue the | ||
// traversal from the head of list B. | ||
ptr_a = if let Some(node) = ptr_a { | ||
node.borrow().next.clone() | ||
} else { | ||
head_b.clone() | ||
}; | ||
// Simultaneously, traverse list B -> list A. | ||
ptr_b = if let Some(node) = ptr_b { | ||
node.borrow().next.clone() | ||
} else { | ||
head_a.clone() | ||
}; | ||
} | ||
// At this point, 'ptr_a' and 'ptr_b' either point to the | ||
// intersection node or both are null if the lists do not | ||
// intersect. Return either pointer. | ||
ptr_a | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#[derive(Debug)] | ||
struct ListNode { | ||
val: i32, | ||
next: Option<Box<ListNode>>, | ||
} | ||
|
||
impl ListNode { | ||
fn new(val: i32) -> Self { | ||
ListNode { | ||
val, | ||
next: None, | ||
} | ||
} | ||
} | ||
|
||
fn linked_list_reversal(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
let mut curr_node = head; | ||
let mut prev_node = None; | ||
|
||
// Reverse the direction of each node's pointer until 'curr_node' | ||
// is null. | ||
while let Some(mut current) = curr_node { | ||
let next_node = current.next.take(); | ||
current.next = prev_node; | ||
prev_node = Some(current); | ||
curr_node = next_node; | ||
} | ||
|
||
// 'prev_node' will be pointing at the head of the reversed linked | ||
// list. | ||
prev_node | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#[derive(Debug)] | ||
struct ListNode { | ||
val: i32, | ||
next: Option<Box<ListNode>>, | ||
} | ||
|
||
impl ListNode { | ||
fn new(val: i32) -> Self { | ||
ListNode { val, next: None } | ||
} | ||
} | ||
|
||
fn linked_list_reversal_recursive(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
// Base cases. | ||
if head.is_none() || head.as_ref().unwrap().next.is_none() { | ||
return head; | ||
} | ||
// Recursively reverse the sublist starting from the next node. | ||
let mut current = head.unwrap(); | ||
let next = current.next.take(); | ||
let new_head = linked_list_reversal_recursive(next); | ||
// Connect the reversed linked list to the head node to fully | ||
// reverse the entire linked list. | ||
if let Some(mut next_node) = new_head { | ||
next_node.next = Some(current); | ||
Some(next_node) | ||
} else { | ||
Some(current) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave the
MultiLevelListNode
commented out and import it from module/folder calledds
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing for other problems (if it's commented out in the python solution)