rust学习笔记16-206.反转链表(递归)

rust函数递归在14中已经提到,接下来我们把206.反转链表,用递归法实现

递归函数通常包含两个主要部分:

基准条件(Base Case):递归终止的条件,避免无限递归。

递归步骤(Recursive Step):将问题分解为更小的子问题,并调用自身来解决这些子问题。

rust 复制代码
 //Definition for singly-linked list.
 #[derive(PartialEq, Eq, 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
     }
   }
 }



pub fn reverse(mut pre : Option<Box<ListNode>>,mut cur : Option<Box<ListNode>>) -> Option<Box<ListNode>> {
    if let Some(mut node) = cur.take() {
        //如果不为空使用temp先保存node.next, 然后让node.next指向pre
        let mut temp = node.next;
        node.next = pre;
        //递归调用
        return reverse(Some(node), temp);    
    } else {
        pre
    }
}
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
  
    let mut pre: Option<Box<ListNode>> = None;

    return reverse(pre, head);
}





// 辅助函数:打印链表内容不转移所有权
fn print_list(head: Option<&Box<ListNode>>) {
    match head {
        Some(node) => {
            let mut current = Some(node); // 初始化当前节点指针
            while let Some(node) = current {
                print!("{} -> ", node.val);
                current = node.next.as_ref(); // 使用 as_ref() 获取对 next 的引用
            }
            println!("None");
        }
        None => {
            println!("链表为空");
        }
    }
}

// 函数:将 i32 数组转换为链表并返回头节点
fn array_to_list(arr: Vec<i32>) -> Option<Box<ListNode>> {
    let mut head: Option<Box<ListNode>> = None;
    let mut current: &mut Option<Box<ListNode>> = &mut head;

    for &val in arr.iter().rev() { // 从后往前构建链表
        let new_node = Box::new(ListNode {
            val,
            next: current.take(), // 取出当前节点并设置为新节点的 next
        });
        *current = Some(new_node); // 更新当前节点
        current = &mut head;       // 指向头节点
    }

    head
}

fn main() { 

    let arr = vec![1, 2, 3, 4, 5];

    // 调用函数创建链表
    let head = array_to_list(arr);

    // 打印链表
    print_list(head.as_ref()); // 使用 as_ref() 避免转移所有权
    let node = reverse_list(head);
    print_list(node.as_ref());

}

总结,用递归首先需要确定终止条件,在翻转链表中,终止条件就是cur为空,然后返回pre, 如果不为空先保存node.next(cur.next)到临时变量temp中,然后node.next=pre,最后递归直到为空返回

相关推荐
ZK_H11 小时前
MFC程序开发自学笔记其一——windows应用程序与c++基础
c++·笔记·mfc
星夜夏空9911 小时前
FreeRTOS学习(10)——消息队列
学习
GLDbalala11 小时前
GPU PRO 5 - 2.6 Wire Antialiasing 笔记
笔记
星幻元宇VR12 小时前
消防安全教育体验展厅设备【模拟灭火系统】
科技·学习·安全
RD_daoyi12 小时前
Google SEO第三周:网站站内基础优化——决定排名快慢的核心基建
大数据·人工智能·学习·搜索引擎·百度·googlecloud
MartinYeung512 小时前
[论文学习]大型语言模型的安全性、安全与隐私问题综述:核心挑战、攻击防禦与未来方向分析
人工智能·学习·安全·语言模型
Ricky055312 小时前
基于对比学习的卫星影像目标检测领域适应方法(2024年美国研究)
人工智能·学习·目标检测
梦0712 小时前
学习笔记-ClaudeCode快速安装配置上手
笔记·学习
江华森12 小时前
TDengine 时序数据库深度学习笔记
笔记·时序数据库·tdengine
路人蛃12 小时前
【深入理解计算机系统】第二章第一节(信息存储)笔记
服务器·网络·笔记·计算机网络·系统架构