• 【LeetCode】21. Middle of the Linked List· 链表的中间节点


    题目描述

    英文版描述

    Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node.

    英文版地址

    https://leetcode.com/problems/middle-of-the-linked-list/

    中文版描述

    给定一个头结点为 head 的非空单链表,返回链表的中间结点。 如果有两个中间结点,则返回第二个中间结点。

    示例 1:

    输入:[1,2,3,4,5]

    输出:此列表中的结点 3 (序列化形式:[3,4,5]) 返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。

    注意,我们返回了一个 ListNode 类型的对象 ans,这样: ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.

    示例 2:

    输入:[1,2,3,4,5,6]

    输出:此列表中的结点 4 (序列化形式:[4,5,6]) 由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。

    提示:

    • 给定链表的结点数介于 1 和 100 之间

    中文版地址

    https://leetcode.cn/problems/middle-of-the-linked-list/

    解题思路

    由于无法直接获取到链表的长度,所以我们需要先从链表头遍历到链表尾,获取到链表的长度,然后找到他的中间节点。

    解题方法

    俺这版

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode middleNode(ListNode head) {
    13. int len = 0;
    14. ListNode myHead = head;
    15. while (myHead != null) {
    16. len++;
    17. myHead = myHead.next;
    18. }
    19. int index = len / 2;
    20. for (int i = 1; i <= index; i++) {
    21. head = head.next;
    22. }
    23. return head;
    24. }
    25. }

    复杂度分析

    官方版

    官方提供了三种方法

    数组

    这个方法就是空间换时间的感觉

    1. class Solution {
    2. public ListNode middleNode(ListNode head) {
    3. ListNode[] A = new ListNode[100];
    4. int t = 0;
    5. while (head != null) {
    6. A[t++] = head;
    7. head = head.next;
    8. }
    9. return A[t / 2];
    10. }
    11. }

    复杂度分析

    • 时间复杂度:O(N),其中 N 是给定链表中的结点数目。

    • 空间复杂度:O(N),即数组 A 用去的空间。

    单指针法

    1. class Solution {
    2. public ListNode middleNode(ListNode head) {
    3. int n = 0;
    4. ListNode cur = head;
    5. while (cur != null) {
    6. ++n;
    7. cur = cur.next;
    8. }
    9. int k = 0;
    10. cur = head;
    11. while (k < n / 2) {
    12. ++k;
    13. cur = cur.next;
    14. }
    15. return cur;
    16. }
    17. }

    复杂度分析

    • 时间复杂度:O(N),其中 NN 是给定链表的结点数目。

    • 空间复杂度:O(1),只需要常数空间存放变量和指针。

    快慢指针法

    思路和算法

    我们可以继续优化方法二,用两个指针 slow 与 fast 一起遍历链表。 slow 一次走一步,fast 一次走两步 那么当 fast 到达链表的末尾时,slow 必然位于中间

    1. class Solution {
    2. public ListNode middleNode(ListNode head) {
    3. ListNode slow = head, fast = head;
    4. while (fast != null && fast.next != null) {
    5. slow = slow.next;
    6. fast = fast.next.next;
    7. }
    8. return slow;
    9. }
    10. }

    复杂度分析

    • 时间复杂度:O(N),其中 N 是给定链表的结点数目。

    • 空间复杂度:O(1),只需要常数空间存放 slow 和 fast 两个指针。

    总结

    在解题思路中提到的方法是我最直接的解题思路(就是官方的单指针法),后面也有考虑要不要优化下,比如:自定一个数组,每便利一个节点,把该点的值存到对应位置的数组中,结束后,无需再次遍历链表,直接从数组中获取呀对应序号的值即可(也就是官方的数组法),但感觉这个方法相比较单指针法其实也就是空间换时间,在空间比较珍贵的情况下,反而不见得能称为“优化”。 快慢指针,,绝了!!!

  • 相关阅读:
    【WebGIS实例】(11)Cesium自定义区域裁剪(挖除&挖出)
    zemax色差与消色差
    Java版企业电子招标采购系统源码—企业战略布局下的采购寻源
    相控阵天线(四):阵列天线波束赋形(遗传算法、粒子群算法、进化差分算法)
    ubuntu升级python到python3.11(可能是全网最靠谱的方法,亲测有效)
    R语言12篇文章带您深入了解限制立方条图(Restricted cubic spline,RCS)
    leetCode-栈类型详解
    NIO流—理解Buffer、Channel概念和NIO的读写操作
    SpringBoot日志管理
    微信小程序wxml使用过滤器
  • 原文地址:https://blog.csdn.net/aqin1012/article/details/127093433