-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
target |
This file contains 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 @@ | ||
target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,6 @@ | ||
[package] | ||
name = "list" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains 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,103 @@ | ||
|
||
|
||
// Definition for singly-linked list. | ||
#[derive(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 | ||
} | ||
} | ||
} | ||
|
||
struct Solution(); | ||
impl Solution { | ||
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
let mut cur = head?; | ||
if let Some(mut next) = cur.next { | ||
cur.next = Solution::swap_pairs(next.next); | ||
next.next = Some(cur); | ||
return Some(next) | ||
} | ||
Some(cur) | ||
} | ||
|
||
|
||
pub fn reverse_between(head: Option<Box<ListNode>>, left: i32, right: i32) -> Option<Box<ListNode>> { | ||
|
||
let mut head = head; | ||
let mut left_prev_node = &mut head; | ||
for _ in 0..left-2{ | ||
left_prev_node = &mut left_prev_node.as_mut().unwrap().next; | ||
} | ||
let mut left_prev_node_ptr = left_prev_node.as_mut().unwrap(); | ||
// if let Some(left_node_ptr) = left_node { | ||
left_prev_node_ptr.next = Solution::reverse(left_prev_node_ptr.next.take()); | ||
// } | ||
|
||
head | ||
|
||
} | ||
|
||
|
||
|
||
pub fn _reverse(prev: Option<Box<ListNode>>, cur: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
let mut cur_node = cur?; | ||
let next = cur_node.next; | ||
cur_node.next = prev; | ||
if next.is_none() { | ||
return Some(cur_node); | ||
} | ||
Solution::_reverse(Some(cur_node), next) | ||
} | ||
|
||
pub fn reverse(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
Solution::_reverse(None, head) | ||
} | ||
|
||
|
||
pub fn print_list(head: &Option<Box<ListNode>>) { | ||
let mut cur = head; | ||
while let Some(cur_ptr) = cur { | ||
print!("{} ",cur_ptr.val); | ||
cur = &cur_ptr.next; | ||
} | ||
println!() | ||
} | ||
} | ||
|
||
fn main() { | ||
// let list = None; | ||
let list = Some(Box::new(ListNode{ | ||
val: 1, | ||
// next: None, | ||
next: Some(Box::new(ListNode{ | ||
val:2, | ||
next: Some(Box::new(ListNode{ | ||
val:3, | ||
// next: None, | ||
next:Some(Box::new(ListNode{ | ||
val:4, | ||
next: Some(Box::new(ListNode{ | ||
val: 5, | ||
next: None | ||
})) | ||
})) | ||
})) | ||
})) | ||
})); | ||
|
||
Solution::print_list(&list); | ||
let new_list = Solution::reverse_between(list, 2,4); | ||
Solution::print_list(&new_list); | ||
|
||
|
||
|
||
} |