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

【链表之练习题】

文章目录

  • 翻转链表
  • 找到链表的中间节点
  • 返回倒数第k个节点
  • 合并两个有序链表
  • 判断链表是否回文
  • 注意


翻转链表

在这里插入图片描述

    //反转链表//实质上是把每一个节点头插法,原本第一个节点变成最后一个节点public ListNode reverseList(){//链表为空if (head == null){return null;}//链表只有一个节点if (head.next == null ){return head;}ListNode cur = this.head.next;//先定义cur的位置this.head.next =null;//再把head.next置为空while(cur != null){ListNode curNext =cur.next;//再定义curNext在cur后面cur.next =this.head;//让cur的下一个等于头节点,就能把cur头插到head前面head = cur;//head给到curcur = curNext;//cur再到curNext位置}return head;//返回头,就能返回一整个链表}
}

找到链表的中间节点

方法1:链表长度除以2得到中间节点
在这里插入图片描述

    //求链表中间节点//1.先求整个链表的长度//2.再求长度/2 就找到这个节点了public ListNode MiddleNode(){ListNode cur = this.head;int len = size();//让cur走到中间节点for (int i = 0; i < len/2; i++) {cur = cur.next;}return cur;}

方法2:
优化代码:快慢指针的引用

  1. 当fast == null时,遍历结束

在这里插入图片描述

  1. 当fast.next == null时,遍历结束

在这里插入图片描述
所以循环结束有两个条件:fast == null 或者 fast.next == null

    public ListNode MiddleNode1(){ListNode fast = this.head;ListNode slow = this.head;while (fast != null && fast.next != null ){fast = fast.next.next;slow = slow.next;}return slow;}

返回倒数第k个节点

在这里插入图片描述

    public ListNode findKthToTail(int k){//判断k的合法性if (k <= 0 || head == null){return null;}ListNode fast =head;ListNode slow =head;//先让fast走k-1步while(k-1 != 0){fast = fast.next;//如果k很大,这个判断可以让代码更高效if (fast == null){return null;}k--;}//slow和fast同时走//当fast.next =null时,slow已经在倒数第k个节点了while(fast.next != null){fast = fast.next;slow = slow.next;}return slow;}

合并两个有序链表

在这里插入图片描述
在这里插入图片描述

public class Test {//定义两个链表public static MySingleList.ListNode  mergeTwoLists(MySingleList.ListNode head1, MySingleList.ListNode head2){//定义一个虚拟节点,保存合并之后的新链表MySingleList.ListNode newH = new MySingleList.ListNode(-1);//newH节点是新链表的头节点,跟着记录串联之后的节点MySingleList.ListNode tmpH = newH;//当两个链表都不为空才能进入循环进行合并排序while(head1 != null && head2 != null){//当head1的值小于head2时,头节点tmpH的下一个节点就是连接小的那一个,然后head1再往后走一步           if (head1.val < head2.val){tmpH.next = head1;head1 = head1.next;}else{//当head2的值小于head1时,头节点tmpH的下一个节点就是连接小的那一个,然后head2再往后走一步  tmpH.next = head2;head2 = head2.next;}//无论进入那个语句,tmp都会往后走一步tmpH = tmpH.next;}//当head1没走完了,说明head2走完了,继续接着剩下的head1if(head1 != null){tmpH.next = head1;}//当head2没走完了,说明head1走完了,继续接着剩下的head2if(head2 != null){tmpH.next = head2;}//最后返回return tmpH;}public static void main(String[] args) {MySingleList mySingleList = new MySingleList();mySingleList.addLast(12);mySingleList.addLast(23);mySingleList.addLast(34);mySingleList.addLast(45);mySingleList.display();//打印数组MySingleList mySingleList2 = new MySingleList();mySingleList2.addLast(15);mySingleList2.addLast(24);mySingleList2.addLast(37);mySingleList2.addLast(166);mySingleList2.display();MySingleList.ListNode head = mergeTwoLists(mySingleList.head,mySingleList2.head);mySingleList.display();}

判断链表是否回文

1.先找到中间节点
2.翻转
3.前面往后走,后面往前走,值是否一样
在这里插入图片描述

    //判断是否回文public boolean chkPalindrome(){ListNode fast = head;ListNode slow = head;int len = size()/2;//5/2=2//1.找中间位置while(fast != null && fast.next != null){//fast先走俩步,slow再走一步fast = fast.next.next;slow = slow.next;}//2.翻转ListNode cur = slow.next;while(cur != null){ListNode curNext = cur.next;cur.next = slow;slow = cur;cur = curNext;}//3.从前到后,从后到前while(head != slow){if (head.val != slow.val){return false;}//考虑偶数链表if (head.next == slow){return true;}head = head.next;slow = slow.next;}return true;}

注意

在写题过程中,我混淆了找中间节点和返回倒数第k个节点的方法

他们的区别其实是:
找中间节点 :fast永远是slow的二倍,slow走一步,fast就走两步。

返回倒数第k个节点 :fast和slow的关系不是固定的,fast走几步根据k-1得到,还有一个区别是,fast走了k-1步之后,slow走一步,fast也是走一步。

http://www.lryc.cn/news/242393.html

相关文章:

  • 情感对话机器人的任务体系
  • 【笔记 Pytorch 08】深度学习模板 (未完)
  • 【如何学习Python自动化测试】—— Cookie 处理
  • IOS+Appium+Python自动化全实战教程
  • 华硕灵耀XPro(UX7602ZM)原装Win11系统恢复安装教程方法
  • SpringBoot整合Redis,redis连接池和RedisTemplate序列化
  • 学习课题:逐步构建开发播放器【QT5 + FFmpeg6 + SDL2】
  • Linux 6.7全面改进x86 CPU微码加载方式
  • 【Python】Fastapi swagger-ui.css 、swagger-ui-bundle.js 无法加载,docs无法加载,redocs无法使用
  • 算法-中等-链表-两数相加
  • STC单片机选择外部晶振烧录程序无法切换回内部晶振导致单片机不能使用
  • 使用STM32+SPI Flash模拟U盘
  • 【自主探索】基于 frontier_exploration 的单个机器人自主探索建图
  • 模板初阶(1):函数模板,类模板
  • AIGC: 关于ChatGPT中生成输出表格/表情/图片/图表这些非文本的方式
  • gen_arrow_contour_xld
  • 智能时代的智能工具(gpt)国产化助手
  • 量子计算 | 解密著名量子算法Shor算法和Grover算法
  • 缓存组件状态,提升用户体验:探索 keep-alive 的神奇世界
  • 万字长文 - Python 日志记录器logging 百科全书 - 高级配置之 日志文件配置
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • OpenGL 绘制旋转球(Qt)
  • 解决:javax.websocket.server.ServerContainer not available 报错问题
  • 81基于matlab GUI的图像处理
  • 虚拟机系列:vmware和Oracle VM VirtualBox虚拟机的区别,简述哪一个更适合我?以及相互转换
  • Go lumberjack 日志轮换和管理
  • git常用命令(git github ssh)
  • 完美解决:Nginx访问PHP出现File not found.
  • 音视频5、libavformat-2
  • python opencv -模板匹配