算法记录——链表

devtools/2024/9/25 20:59:54/

2.链表

2.1判断是否是回文链表

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 boolean isPalindrome(ListNode head) {Stack<ListNode> listNodes = new Stack<>();ListNode p = head;//利用栈反转链表,判断是否是回文链表while (p != null) {//将链表中所有元素入栈listNodes.push(p);p = p.next;}while (!listNodes.empty()) {if (listNodes.pop().val == head.val) {//head = head.next;} else {return false;}}return true;}
}

2.方法2:利用快慢指针

/*** 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) {//代表快指针,一次走两步ListNode fast = head;//代表慢指针,一次走一步ListNode slow = head;while (fast.next != null && fast.next.next != null) {fast = fast.next.next;slow = slow.next;}//退出循环时,如果链表节点是奇数个,快指针在尾节点,慢指针在中点。如果是偶数个,快指针还是在尾节点,慢指针在中点前一个。//把右半部分链表反转slow = reverseList(slow.next);while (slow != null) {if (head.val != slow.val) return false;//值不相同,直接返回falsehead = head.next;slow = slow.next;}return true;}//反转链表public static ListNode reverseList(ListNode head) {ListNode cur = head;ListNode pre = null;while (cur != null) {ListNode temp = cur.next;cur.next = pre;pre = cur;cur = temp;}return pre;}
}

2.2 模板题:反转链表

/*** 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 reverseList(ListNode head) {//cur:用于遍历链表元素的指针,代表当前遍历到的节点,初始化当然为head了ListNode cur = head;//pre:代表当前cur节点,反转后应该指向的节点。因为cur初始在head,反转以后就是尾节点了指向null,所以pre初始化为nullListNode pre = null;while(cur != null){//当元素还没遍历完的时候//在cur指向pre前,用于保存cur.next,防止链表找不到了。ListNode temp = cur.next;//让当前节点cur,指向precur.next = pre;//让pre变为反转链表的最前面一个节点pre = cur;//让cur移动到原链表的头节点cur = temp;}// 注意:pre的含义还是反转链表的头节点!return pre;}
}

复杂度分析:
时间复杂度 O(N)O(N)O(N) : 遍历链表使用线性大小时间。
空间复杂度 O(1)O(1)O(1) : 变量 pre 和 cur 使用常数大小额外空间。

已经是最优的解法了,还有一种递归方法就不赘述了。

2.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 ListNode partition(ListNode head, int x) {
if (head == null || head.next == null) return head;//代表小于目标值区域的头和尾ListNode h1 = null;ListNode t1 = null;//代表大于等于目标值的头和尾ListNode h2 = null;ListNode t2 = null;//用于保存head的下一个节点//注意:这里最后拼接好了以后,小于区域的头就是整个链表的新的头节点,因此,head可以作为遍历链表的指针。ListNode next = head.next;while (head != null) {//遍历next = head.next;head.next = null;if (head.val < x) {//如果当前节点的val小于目标值if (h1 == null) {//如果当前节点是小于区域的第一个节点h1 = head;t1 = head;} else {t1.next = head;t1 = head;}} else {if (h2 == null) {//如果当前节点是大于区域的第一个节点h2 = head;t2 = head;} else {//其他情况就把该节点尾插法插入链表中t2.next = head;t2 = head;}}head = next;}//进行小于区域链表和大于等于区域链表的拼接if (h2 == null) {//如果没有大于等于区域return h1;}if (h1 == null) {//如果没有小于区域return h2;}//如果两种区域都有,则让小于区域的尾指针指向大于等于区域的头指针t1.next = h2;return h1;}
}

2.4 随机链表的赋值

/*
// Definition for a Node.
class Node {int val;Node next;Node random;public Node(int val) {this.val = val;this.next = null;this.random = null;}
}
*/class Solution {public Node copyRandomList(Node head) {//创建一个map,key为老链表的节点。val为新链表的节点HashMap<Node,Node> map = new HashMap<Node,Node>();Node cur = head;//遍历链表,设置map的key和valuewhile(cur != null){map.put(cur,new Node(cur.val));cur = cur.next;}cur = head;//再次遍历老链表,给新链表设置每一个节点的next和randomwhile(cur != null){//cur 老链表节点//map.get(cur) cur对应的新链表map.get(cur).next = map.get(cur.next);//设置新链表的nextmap.get(cur).random = map.get(cur.random);//设置新链表的randomcur = cur.next;}return map.get(head);}
}

2.5环形链表的判断

方法一:利用HashSet集合。

思路:遍历当前链表,每次遍历判断当前节点是否已经存在于set集合中。如果不存在,则把当前节点放入集合。如果已经存在,说明当前节点就是第一个入环节点。

/*** 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) {//创建一个set,用于存放链表中已经遍历了的节点HashSet<ListNode> set = new HashSet<>();while(head != null){//如果当前节点已经存在于set,说明存在环形结构if(set.contains(head)) return true;set.add(head);head = head.next;}return false;}
}

方法二:快慢指针

开始时,快慢指针都在头节点的位置。快指针一次走两步,慢指针一次走一步。

如果没有环结构,快指针一定先走到尾节点。

如果有环结构,快慢指针会在换里相遇。而相遇所要走的卷数不会大于两圈。

相遇以后,快指针/慢指针到头节点的位置。两个指针开始一次走一步。最终两个指针会在第一次入换节点相遇!(原理就不证明了)

/*** 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) {if(head == null || head.next == null || head.next.next == null) return null;//定义慢指针,一次走一步ListNode n1 = head.next;//定义快指针,一次走两步ListNode n2 = head.next.next;while(n1 != n2){//当n1 n2不相遇时循环,所以我开始时没有把两个指针都设置在头节点的位置if(n2.next == null || n2.next.next == null){//说明没有环结构,直接返回空return null;}n1 = n1.next;//慢指针一次走一步n2 = n2.next.next;//慢指针一次走两步}//快指针移到头节点,开始一次走一步n2 = head;while(n1 != n2){//当两个指针相遇时,就走到了第一个入环节点n1 = n1.next;n2 = n2.next;}return n1;}
}

2.6 链表相交

思路:两个单链表,如何判断有没有相交点呢?

1.先遍历两个链表,到尾节点时停止。如果这时候,两个链表的尾节点都不想等。说明二者不相交。

2.如果二者尾节点是同一个,则计算二者链表长度的差值。让长的链表先走差值个距离。然后,短的链表从头开始走,二者一定会在相交点相遇!

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {//定义两个指针用于遍历两条链表ListNode cur1 = headA;ListNode cur2 = headB;int n = 0;//用于记录两条链表的差值while(cur1.next != null){cur1 = cur1.next;n++;}while(cur2.next != null){cur2 = cur2.next;n--;}if(cur1 != cur2){//尾节点都不想等,说明二者不相交return null;}//这样遍历完两条链表,n就是两条链表的长度差cur1 = n > 0 ? headA : headB;//让cur1指向两条链表中长的那一条cur2 = cur1 == headA ? headB : headA;//让cur2指向两条链表中短的那一条n = Math.abs(n);//n取绝对值while(n != 0){//让长的那条链表先移动两条链表差值的距离,再一起走,就会在相交部分汇合!cur1 = cur1.next;n--;}while(cur1 != cur2){cur1 = cur1.next;cur2 = cur2.next;}return cur1;}
}

2.7.两数相加

思路:

/*** 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 addTwoNumbers(ListNode l1, ListNode l2) {ListNode res = new ListNode();ListNode cur = res;int carry = 0;//当l1、l2中有一个不是空节点,或者还有进位,就继续循环while (l1 != null || l2 != null || carry != 0) {if (l1 != null) carry += l1.val;if (l2 != null) carry += l2.val;cur.next = new ListNode(carry % 10);//carry%10 就是该点的valcur = cur.next;carry = carry / 10;// carry/10 就是下一个点的进位if (l1 != null) l1 = l1.next;//l1 没有遍历完if (l2 != null) l2 = l2.next;}return res.next;}
}

2.8.合并两个有序链表

/*** 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 mergeTwoLists(ListNode list1, ListNode list2) {ListNode res = new ListNode();ListNode cur = res;while (list1 != null && list2 != null) {if (list1.val <= list2.val) {//如果l1链表的值更小cur = cur.next = list1;list1 = list1.next;} else {cur = cur.next = list2;list2 = list2.next;}}while (list1 != null) {//如果1还没遍历完cur = cur.next = list1;list1 = list1.next;}while (list2 != null) {//如果2还没遍历完cur = cur.next = list2;list2 = list2.next;}return res.next;}
}

2.9 反转链表2

题解参考:leetcode

/*** 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 reverseBetween(ListNode head, int left, int right) {ListNode dummy = new ListNode(0, head);ListNode g = dummy;ListNode p = dummy;//g指向的下一个节点就是要开始反转的节点for (int i = 0; i < left - 1; i++) {g = g.next;p = p.next;}//p指向第left个节点p = p.next;for (int i = 0; i < right - left; i++) {ListNode temp = p.next;p.next = p.next.next;temp.next = g.next;g.next = temp;}return dummy.next;}
}

2.10.K个一组反转链表

        本题思路和上一题差不多。举一反三,还是主要用g、p两个指针反转链表

        每组链表反转之前,g的next指向的都是待反转链表的第一个节点,p指向的就是待反转链表的第一个节点。
        要注意的就是每次反转完链表p指针指向的就是反转后链表的最后一个元素,同时它的next也是下一组待反转链表的第一个元素,所以每次每组反转完以后,都要把p赋值给g。

/*** 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 reverseKGroup(ListNode head, int k) {ListNode dummy = new ListNode(0, head);ListNode g = head;//计算一共有多少个节点,用来算要反转几组链表int count = 0;while (g != null) {g = g.next;count++;}g = dummy;ListNode p = g.next;//遍历for (int i = 0; i < count / k; i++) {p = g.next;//反转的每组链表for (int j = 1; j < k; j++) {ListNode temp = p.next;p.next = p.next.next;temp.next = g.next;g.next = temp;}//每组链表反转完,让cur的next指向下一组待反转链表第一个g = p;}return dummy.next;}
}


http://www.ppmy.cn/devtools/117162.html

相关文章

如何用Shell命令结合 正则表达式 统计文本中的ip地址数量

文章目录 简介问题回答 简介 IP 地址&#xff08;Internet Protocol Address&#xff09;是互联网协议地址的简称&#xff0c;是互联网上为联网的设备&#xff08;如计算机、服务器、路由器、手机等&#xff09;分配的唯一标识符。IP 地址的主要功能是实现不同网络设备之间的通…

Hive安装教程

前提条件 已经安装好hadoop集群以及mysql CentOS7搭建Hadoop3集群教程 CentOS7安装MySQL教程 下载hive hive3.1.3下载连接&#xff1a;https://archive.apache.org/dist/hive/hive-3.1.3/apache-hive-3.1.3-bin.tar.gz 登录master服务器hadoop&#xff0c;将压缩包上传到…

群晖使用Docker部署WPS Office并实现异地使用浏览器制作办公文档

文章目录 前言1. 本地环境配置2. 制作本地分享链接3. 制作公网访问链接4. 公网ip地址访问您的分享相册5. 制作固定公网访问链接 前言 想象一下这个场景&#xff1a;如果遇到周末紧急需要改方案&#xff0c;但团队成员都在各自家中&#xff0c;这个时候如果大家能够轻松访问这个…

Stable Diffusion绘画 | ControlNet应用-qrcode 二维码控制器:艺术二维码来啦

qrcode 二维码控制器&#xff0c;是一款专用于生成艺术二维码的控制器&#xff0c; 需要单独下载&#xff0c;下载后&#xff0c;将文件放置在&#xff1a;SD安装目录\extensions\sd-webui-controlnet\models 实操 开启第一个 ControlNet&#xff0c;上传一个二维码图片&…

解决nginx代理SSE接口的响应没有流式返回

目录 现象原来的nginx配置解决 现象 前后端分离的项目&#xff0c;前端访问被nginx反向代理的后端SSE接口&#xff0c;预期是流式返回&#xff0c;但经常是很久不响应&#xff0c;一响应全部结果一下子都返回了。查看后端项目的日志&#xff0c;响应其实是流式产生的。推测是n…

ER 图 Entity-Relationship (ER) diagram 101 电子商城 数据库设计

起因&#xff0c; 目的: 客户需求, 就是要设计一个数据库。 过程&#xff0c; 关于工具: UI 设计&#xff0c;我最喜欢的工具其实是 Canva, 但是 Canva 没有合适的模板。我用的是 draw.io, 使用感受是&#xff0c;很垃圾。 各种快捷键不适应&#xff0c;箭头就是点不住&…

汽车3d动画渲染选择哪个?选择最佳云渲染解决方案

面临汽车3D动画渲染挑战&#xff1f;选择正确的云渲染服务至关重要。探索最佳解决方案&#xff0c;优化渲染效率&#xff0c;快速呈现逼真动画。 汽车3d动画渲染选择哪个&#xff1f; 对于汽车3D动画渲染&#xff0c;选择哪个渲染器取决于你的项目需求、预算和期望的效果。Ble…

webview2加载本地页面

加载方式 通过导航到文件 URL 加载本地内容 使用方式&#xff1a; webView->Navigate( L"file:///C:/Users/username/Documents/GitHub/Demos/demo-to-do/index.html"); 但是这种方式存在一些问题&#xff0c;比如&#xff1a; 存在跨域问题&#xff08;我加载…