zoukankan      html  css  js  c++  java
  • 带环链表 linked list cycle

    [抄题]:

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    反正链表的题都得先提出特殊情况就对了

    [复杂度]:Time complexity: O() Space complexity: O()

    [算法思想:迭代/递归]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

    /**
     * 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) {  
            //先提出特殊情况
             if (head == null || head.next == null) {
                    return false;
                }
            
            //定义
            ListNode slow = head;
            ListNode fast = head.next;
    
            while (slow != fast) {
                //如果在里面fast到头了,就返回false
                if (fast == null || fast.next == null) {
                    return false;
                }
                
                slow = slow.next;
                fast = fast.next.next;
            }
            
            //返回
            return true;
        }
    }
    View Code
  • 相关阅读:
    软件过程的守护神
    C++一个简单的手柄类模板
    矿Spring入门Demo
    [Docker] Prune Old Unused Docker Containers and Images
    [Ramda] Compose lenses
    [Docker] Build Your Own Custom Docker Image
    [SCSS] Reuse Styles with the SCSS @mixin Directive
    [Angular] Router outlet events
    [Angular] Subscribing to router events
    [Angular] Enable router tracing
  • 原文地址:https://www.cnblogs.com/immiao0319/p/12928834.html
Copyright © 2011-2022 走看看