基础算法精讲 07|环形链表II,快慢指针

news/2024/12/2 13:40:01/

876. 链表的中间结点 - 力扣(LeetCode)

Time: O(n)

Space:O(1)

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode middleNode(ListNode head) {ListNode slow = head;ListNode fast = head;while (fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;}return slow;}
}

141. 环形链表 - 力扣(LeetCode)

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public boolean hasCycle(ListNode head) {if (head == null || head.next == null) return false;ListNode slow = head;ListNode fast = head;while (fast!= null && fast.next != null) {slow = slow.next;fast = fast.next.next;if (slow == fast) return true;}return false;}
}

142. 环形链表 II - 力扣(LeetCode)

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode detectCycle(ListNode head) {ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;if (slow == fast) break;}if (fast == null || fast.next == null) return null;while (head != slow) {head = head.next;slow = slow.next;}return head;}
}

143. 重排链表 - 力扣(LeetCode)

1. 找mid

2.reverse

3.重排

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public void reorderList(ListNode head) {if (head == null || head.next == null) return;ListNode first = head;ListNode mid = findMid(head);ListNode second = mid.next;mid.next = null;second = reverse(second);reorder(first, second);return;}private ListNode findMid(ListNode head) {ListNode fast = head;ListNode slow = head;while (fast.next != null && fast.next.next != null) {fast = fast.next.next;slow = slow.next;}return slow;}private ListNode reverse(ListNode head) {if (head == null || head.next == null) return head;ListNode newHead = reverse(head.next);head.next.next = head;head.next = null;return newHead;}private ListNode reorder(ListNode first, ListNode second) {ListNode dummy = new ListNode(0);ListNode cur = dummy;while (first != null && second != null) {cur.next = first;first = first.next;cur.next.next = second;second = second.next;cur = cur.next.next;}if (first != null) {cur.next = first;}if (second != null) {cur.next = second;}return dummy.next;}
}

课后作业:

234. 回文链表 - 力扣(LeetCode)

1.找中间节点

2.reverse后半段

3.对比两段是不是一样

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public boolean isPalindrome(ListNode head) {if (head == null || head.next == null) return true;ListNode first = head;ListNode mid = findMid(head);ListNode second = mid.next;mid.next = null;second = reverse(second);return compare(first, second);}private ListNode findMid(ListNode head) {ListNode fast = head;ListNode slow = head;while (fast.next != null && fast.next.next != null) {fast = fast.next.next;slow = slow.next;}return slow;}private ListNode reverse(ListNode head) {if (head == null || head.next == null) return head;ListNode newHead = reverse(head.next);head.next.next = head;head.next = null;return newHead;}private boolean compare(ListNode first, ListNode second) {while (first != null && second != null) {if (first.val != second.val) return false;first = first.next;second = second.next;}return true;}}


http://www.ppmy.cn/news/1408948.html

相关文章

python爬虫获取豆瓣前top250的标题(简单)

今天是简略的一篇,简单小实验 import requests from bs4 import BeautifulSoup# 模拟浏览器的构成(请求头) headers {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Ch…

github本地仓库push到远程仓库

1.从远程仓库clone到本地 2.生成SSH秘钥&#xff0c;为push做准备 在Ubuntu命令行输入一下内容 [rootlocalhost ~]# ssh-keygen -t rsa < 建立密钥对&#xff0c;-t代表类型&#xff0c;有RSA和DSA两种 Generating public/private rsa key pair. Enter file in whi…

消息中间件之消息通信模型MQ

一&#xff0c;为什么需要MQ&#xff1f; 应用中&#xff0c;经常需要对庞大的海量数据进行监控&#xff0c;随着网络技术和软件开发技术的不断提高&#xff0c;在实战开发中MQ的使用与日俱增&#xff0c;特别是RabbitMQ在分布式系统中存储转发消息&#xff0c;可以保证数据不…

WordPress建站教程:10步快速搭建个人网站

WordPress是一个广泛使用的内容管理系统&#xff08;CMS&#xff09;&#xff0c;凭借其用户友好的界面和大量可定制的主题和插件&#xff0c;为WordPress 提供了多功能性和灵活性&#xff0c;可用于创建各种类型的网站&#xff0c;包括个人博客、B2B企业网站、B2C外贸网站等&a…

一、持续集成介绍

持续集成介绍 一、什么是持续集成二、持续集成的流程三、持续集成的组成要素四、持续集成的好处 一、什么是持续集成 持续集成&#xff08;CI&#xff09;指的是&#xff0c;频繁地&#xff08;一天多次&#xff09;将代码集成到主干。持续集成的目的&#xff0c;就是让产品可…

【C语言自定义类型之----结构体,联合体和枚举】

一.结构体 1.结构体类型的声明 srruct tag {nemer-list;//成员列表 }varible-list;//变量列表结构体在声明的时候&#xff0c;可以不完全声明。 例如&#xff1a;描述一个学生 struct stu {char name[20];//名字int age;//年龄char sex[20];//性别 };//分号不能省略2.结构体…

大模型学习笔记八:手撕AutoGPT

文章目录 一、功能需求二、演示用例三、核心模块流程图四、代码分析1&#xff09;Agent类目录创建智能体对象2&#xff09;开始主流程3&#xff09;在prompt的main目录输入主prompt和最后prompt4&#xff09;增加实际的工具集tools&#xff08;也就是函数&#xff09;5&#xf…

5. 多重背包问题 II(acwing)

文章目录 5. 多重背包问题 II题目描述动态规划一维数组三重循环&#xff08;超时&#xff09;二进制优化&#xff08;正确代码&#xff09; 二维数组三重循环&#xff08;超时&#xff09;二进制优化&#xff08;超出内存限制&#xff09; 5. 多重背包问题 II 题目描述 有 N种…