feat: refactor reverse_list function and add reverse_list_2 implementation

This commit is contained in:
SquidSpirit 2025-08-17 23:33:49 +08:00
parent 11d6a65155
commit 51bfae35cd

View File

@ -1,3 +1,5 @@
use std::ops::Deref;
fn main() {
println!("Hello, world!");
}
@ -22,9 +24,9 @@ impl Solution {
let mut arr: Vec<i32> = vec![];
let mut current_node = head;
while current_node != None {
arr.push(current_node.as_ref().unwrap().val);
current_node = current_node.unwrap().next;
while let Some(node) = current_node.take() {
arr.push(node.val);
current_node = node.next;
}
if arr.is_empty() {
@ -41,4 +43,20 @@ impl Solution {
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
}
}