当前位置: 首页 > news >正文

【学习记录】第六周

一、本周核心学习内容

  1. 链表基础知识复习
    单链表结构回顾:复习了链表节点的基本定义、链表的创建、插入、删除和遍历操作
    链表与数组对比:重新梳理了链表在动态内存分配、插入删除效率等方面的优势
    常见链表操作:包括反转链表、合并有序链表、检测环等基础算法
  2. 双指针技术深入掌握
    本周重点学习了三种双指针应用场景:

快慢指针应用

// 1. 检测链表环structListNode{intval;structListNode*next;};inthasCycle(structListNode*head){if(head==NULL||head->next==NULL){return0;}structListNode*slow=head;structListNode*fast=head;while(fast!=NULL&&fast->next!=NULL){slow=slow->next;// 慢指针走一步fast=fast->next->next;// 快指针走两步if(slow==fast){// 两指针相遇,说明有环return1;}}return0;// 无环}

左右指针应用

// 2. 反转链表(双指针实现)structListNode*reverseList(structListNode*head){structListNode*prev=NULL;structListNode*curr=head;while(curr!=NULL){structListNode*nextTemp=curr->next;// 暂存下一个节点curr->next=prev;// 反转指针方向prev=curr;// 前指针后移curr=nextTemp;// 当前指针后移}returnprev;// 新的头节点}

前后指针应用`

// 3. 删除链表倒数第N个节点structListNode*removeNthFromEnd(structListNode*head,intn){structListNodedummy={0,head};// 虚拟头节点简化边界处理structListNode*first=&dummy;structListNode*second=&dummy;// 让first指针先走n+1步for(inti=0;i<=n;i++){first=first->next;}// 两个指针同步移动,直到first到达末尾while(first!=NULL){first=first->next;second=second->next;}// 删除目标节点structListNode*toDelete=second->next;second->next=second->next->next;returndummy.next;}

快慢指针找链表中点

// 找到链表的中间节点structListNode*findMiddle(structListNode*head){if(head==NULL||head->next==NULL){returnhead;}structListNode*slow=head;structListNode*fast=head;// 快指针每次走两步,慢指针每次走一步while(fast!=NULL&&fast->next!=NULL){slow=slow->next;fast=fast->next->next;}returnslow;// 当快指针到达末尾时,慢指针刚好在中间}

双指针判断回文链表

// 判断链表是否为回文结构intisPalindrome(structListNode*head){if(head==NULL||head->next==NULL){return1;}// 步骤1:找到中点structListNode*slow=head;structListNode*fast=head;while(fast->next!=NULL&&fast->next->next!=NULL){slow=slow->next;fast=fast->next->next;}// 步骤2:反转后半部分链表structListNode*secondHalf=reverseList(slow->next);// 步骤3:比较前后两部分structListNode*p1=head;structListNode*p2=secondHalf;intresult=1;while(result&&p2!=NULL){if(p1->val!=p2->val){result=0;}p1=p1->next;p2=p2->next;}// 步骤4:恢复原链表(可选)slow->next=reverseList(secondHalf);returnresult;}

双指针解决两数相加

// 两个链表表示的非负整数相加structListNode*addTwoNumbers(structListNode*l1,structListNode*l2){structListNodedummy={0,NULL};structListNode*curr=&dummy;intcarry=0;// 进位while(l1!=NULL||l2!=NULL||carry!=0){intsum=carry;if(l1!=NULL){sum+=l1->val;l1=l1->next;}if(l2!=NULL){sum+=l2->val;l2=l2->next;}carry=sum/10;// 计算进位curr->next=(structListNode*)malloc(sizeof(structListNode));curr->next->val=sum%10;// 当前位结果curr->next->next=NULL;curr=curr->next;}returndummy.next;}
http://www.cnnetsun.cn/news/52073.html

相关文章:

  • Cursor高级技巧与最佳实践
  • Cursor + MCP:冲击的不仅是前端,而是整个软件开发范式!
  • 2025年十大旗舰对决:极致轻薄成高端手机新战场
  • 【Vue3】 中 ref 与 reactive:状态与模型的深入理解
  • 毕设 stm32 RFID员工打卡门禁系统(源码+硬件+论文)
  • 全球最大、最领先的吉利全球全域安全中心正式发布
  • Android中Compose系列之按钮Button
  • wangEditor导入excel数据到html富文本编辑
  • 光伏电池simulink仿真模型 光伏电池建模仿真 包括改变温度 改变辐照度的特性分析 模型可...
  • JSP中如何利用分块技术实现百万文件上传优化?
  • 60、Ubuntu 安装硬件规划全攻略
  • 2025年12月— CET四六级答案
  • 锐捷RGSP | 端口安全技术原理与应用
  • Cameralink采集卡软件EspeedGrab使用讲解:4图像处理
  • 31、脚本编程进阶:Here文档、自上而下设计与流程控制
  • 信捷XDH系列PLC的追剪/飞剪/电子凸轮程序模板
  • 【大模型】-LangChain--stream流式同步异步
  • 兜兜英语每日短语:逃单篇
  • 计算机毕业设计springboot汽车智慧检修系统 基于SpringBoot的智能汽车故障预测与维修管理平台 融合IoT的SpringBoot车辆健康监测与维修决策系统
  • python3
  • 【3D图像技术分析与实现】Apple Vision Pro三维成像技术栈深度解析
  • 经典算法题详解之统计重复个数(三)
  • 移动应用开发实验室大一上考核
  • 云数据库服务(如AWS RDS)的优势和考虑因素?
  • 【设计模式|第四篇】适配器模式:让不兼容的接口协同工作
  • asgiref终极指南:高效解决Python异步通信难题
  • 医学影像深度学习知识点总结
  • 从零到一:自动化3D建模的免代码解决方案
  • Kali中生成被控端
  • 13、Linux 文本编辑与命令操作实用指南