• 算法(常用)——快速找到链表的中间节点


    思路:利用快慢指针思想,快指针每次走两步,慢指针走一步。当快指针走到底的时候,满指针指向的就是链表的中间节点。需要注意的是,当链表长度为偶数位的时候,则慢指针指向的是中间偏右的节点,奇数的时候,指向的是中间节点。

            if(head == null || head.next == null) return true;
    
            ListNode fast = head, slow = head;
    
            while(fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
    
    		// 如果 fast == null,则链表长度为偶数,反之则为奇数。
    
  • 相关阅读:
    LOJ#551 Matrix
    洛谷P5163 WD与地图
    洛谷P4831 Scarlet loves WenHuaKe
    LOJ#6118 鬼牌
    支配树
    线性插值
    PropertiesConfiguration
    ThreadLocal
    Thread
    FastJson
  • 原文地址:https://www.cnblogs.com/lippon/p/14117691.html
Copyright © 2020-2023  润新知