feat: refactor reverse_list function and add reverse_list_2 implementation
This commit is contained in:
parent
11d6a65155
commit
51bfae35cd
@ -1,3 +1,5 @@
|
|||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
@ -22,9 +24,9 @@ impl Solution {
|
|||||||
let mut arr: Vec<i32> = vec![];
|
let mut arr: Vec<i32> = vec![];
|
||||||
let mut current_node = head;
|
let mut current_node = head;
|
||||||
|
|
||||||
while current_node != None {
|
while let Some(node) = current_node.take() {
|
||||||
arr.push(current_node.as_ref().unwrap().val);
|
arr.push(node.val);
|
||||||
current_node = current_node.unwrap().next;
|
current_node = node.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if arr.is_empty() {
|
if arr.is_empty() {
|
||||||
@ -41,4 +43,20 @@ impl Solution {
|
|||||||
|
|
||||||
head
|
head
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reverse_list_2(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
|
||||||
|
if head.is_none() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let mut p: Option<Box<ListNode>> = None;
|
||||||
|
|
||||||
|
while let Some(mut node) = head.take() {
|
||||||
|
let next = node.next;
|
||||||
|
node.next = p;
|
||||||
|
p = Some(node);
|
||||||
|
head = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
p
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user