This commit is contained in:
krahets
2025-07-10 07:23:42 +08:00
parent 85ebada8d9
commit 3f088184e4
5 changed files with 41 additions and 37 deletions
+10 -6
View File
@@ -977,13 +977,17 @@ comments: true
}
/* 將 List 轉化為 Array 並返回 */
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
if let Some(node) = head {
let mut nums = self.to_array(node.borrow().next.as_ref());
nums.push(node.borrow().val);
return nums;
pub fn to_array(&self) -> Vec<T> {
fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
if let Some(node) = head {
let mut nums = _to_array(node.borrow().next.as_ref());
nums.push(node.borrow().val);
return nums;
}
return Vec::new();
}
return Vec::new();
_to_array(self.peek())
}
}
```