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

代码随想录day04

24. 两两交换链表中的节点

● 力扣题目链接
● 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
● 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

思路

● 使用迭代的方法,分析交换逻辑即可
○ 注意,可能链表节点个数奇数或偶数,分开讨论
○ 时间复杂度O(n) 空间复杂度O(1)
● 递归方法,时间复杂度O(n) 空间复杂度O(n)
○ 注意保存临时节点

代码

// 迭代
class Solution {public ListNode swapPairs(ListNode head) {if (head == null || head.next == null) return head; // 空或单个节点直接返回ListNode dummy = new ListNode(-1, head);ListNode pre = dummy;ListNode cur = head;ListNode temp;while (cur != null && cur.next != null) { // 节点个数可能为奇数或偶数temp = cur.next;pre.next = temp;cur.next = temp.next;temp.next = cur;pre = cur;cur = pre.next;}return dummy.next;}
}
// 递归
class Solution {public ListNode swapPairs(ListNode head) {if (head == null || head.next == null) return head;ListNode reHead = swapPairs(head.next.next);ListNode temp = head.next;temp.next = head;head.next = reHead;return temp;}
}

19.删除链表的倒数第N个节点

● 力扣题目链接
● 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

思路

● 双指针,快指针先走n步,然后快慢指针一起走,快指针到末尾,慢指针到要删除元素的前一个元素,删除即可
● 使用栈,空间复杂度为O(n) 先依次入栈,然后弹出n个元素,这时栈顶的正好是要删除节点的前一个节点,删除即可(注意虚拟头节点也入栈,不然删除头结点时会空指针)

代码

class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummy = new ListNode(-1, head);ListNode f = dummy, s = dummy;while (n-- > 0) {f = f.next;}while (f.next != null) {f = f.next;s = s.next;}s.next = s.next.next;return dummy.next;}
}
// 使用栈
class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {Deque<ListNode> stack = new ArrayDeque();ListNode dummy = new ListNode(-1, head);ListNode cur = dummy;while (cur != null) {stack.addFirst(cur);cur = cur.next;}for (int i = 0; i < n; i++) {stack.removeFirst();}ListNode pre = stack.peek();pre.next = pre.next.next;return dummy.next;}
}

面试题 02.07. 链表相交

● 力扣题目链接
● 给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

思路

● 先求两个链表的长度,然后通过gap让两个链表尾部对齐,之后同时走,看会不会相遇,就是headA = headB
● 更快的方法,A和B同时遍历,走到空就从另一个链表头部继续遍历,如果相交就到相交节点,如果不相交就到空,时间复杂度O(m+n) 空间复杂度O(1)

代码

public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {int lenA = getLen(headA);int lenB = getLen(headB);if (lenB > lenA) { // B长就交换长度和头结点int tempLen = lenA;lenA = lenB;lenB = tempLen;ListNode tempNode = headA;headA = headB;headB = tempNode;}int gap = lenA - lenB;while (gap-- > 0) { // 尾部对齐headA = headA.next;}while (headA != null) {if (headA == headB) return headA;headA = headA.next;headB = headB.next;}return null;}// 求链表长度private int getLen(ListNode head) {int len = 0;while (head != null) {len++;head = head.next;}return len;}
}public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode tA = headA;ListNode tB = headB;while (tA != tB) {tA = tA != null ? tA.next : headB;tB = tB != null ? tB.next : headA;}return tA;}
}

142.环形链表II

● 力扣题目链接
● 题意: 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
● 为了表示给定链表中的环,使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

思路

● 快慢指针处理即可

代码

public class Solution {public ListNode detectCycle(ListNode head) {ListNode f = head, s = head;while (f != null && f.next != null) { // 快指针一次走两步,注意判空f = f.next.next;s = s.next;if (f == s) { // 相遇,有环f = head; // 快指针回到头结点while (f != s) {f = f.next;s = s.next; // 一起向前走}return f; // 找到入环节点}}return null; // 没有环,返回null}
}
http://www.lryc.cn/news/120888.html

相关文章:

  • [Realtek] WPA_SUPPLICANT + WPA_CLI使用指南
  • # ⛳ Docker 安装、配置和详细使用教程-Win10专业版
  • Linux 教程
  • 图论——最短路算法
  • 在项目中增加网络加载需要考虑什么?
  • 阿里云服务器部署RabbitMQ流程
  • 青大数据结构【2014】
  • Ansible Playbook快速部署一主多从MySQL集群
  • 27.Netty源码之FastThreadLocal
  • linux下离线安装docker
  • SQL server 异地备份数据库
  • 高并发系统设计要点
  • Redis 拒绝服务漏洞(CVE-2023-28856)修复处理
  • Android保存网页的方法
  • P2P 网络,PING程序。
  • OPENCV C++(十二)模板匹配
  • 【配置环境】Linux下安装MySQL
  • 【100天精通python】Day30:使用python操作数据库_数据库基础入门
  • android 如何分析应用的内存(十八)终章——使用Perfetto查看内存与调用栈之间的泄露
  • arcpy实现kml批量转出为shp 包括shp合并
  • 高等数学:泰勒公式
  • JZ32 从上往下打印二叉树(Java)
  • hackNos靶机
  • 取地址及 const取地址操作符重载
  • 【Linux初阶】进程间通信介绍 管道
  • App 在macOS Catalina下提示已损坏无法打开解决办法:
  • ad+硬件每日学习十个知识点(26)23.8.6 (DCDC的降压电路、升压电路、降压-升压电路,同步整流,选型考虑同步、隔离)
  • Elasticsearch3节点集群配置账号密码安全验证
  • ffmepg滤镜
  • Linux 基础(六)常用命令 - find locate which whereis gzip gunzip tar