zoukankan      html  css  js  c++  java
  • 11.判断单链表是否有环

    判断单链表是否有环: 
      这里也是用到两个指针,如果一个链表有环,那么用一个指针去遍历,是永远走不到头的。 
      因此,我们用两个指针去遍历:first指针每次走一步,second指针每次走两步,如果first指针和second指针相遇,说明有环。时间复杂度为O (n)。 方法

        // 方法:检测单链表是否有环
        public boolean hasCycle(Node head) {
            if (head == null) {
                return false;
            }
            Node first = head;
            Node second = head;
            while (second != null) {
                first = first.next;
                second = second.next.next;
                if (first == second) {//一旦两个指针相遇,说明链表是有环的
    return true;
                }
            }
            return false;
    
        }

    完整版代码:(包含测试部分)

    public class LinkListCycle {
        public Node head;
        public Node current;
    
        // 向链表中添加数据
        public void add(int data) {
            // 判断链表为空的时候
            if (head == null) {// 如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头节点
                head = new Node(data);
                current = head;
            } else {
                current.next = new Node(data);// 创建新的结点,放在当前节点的后面(把新的节点和链表进行关联)
                current = current.next;// 把链表的当前索引向后移动一位,此步操作完成之后,current结点指向新添加的那个结点
            }
        }
    
        // 方法重载:向链表中添加结点
        public void add(Node node) {
            if (node == null) {
                return;
            }
            if (head == null) {
                head = node;
                current = head;
            } else {
                current.next = node;
                current = current.next;
            }
        }
    
        // 方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历
        public void print(Node node) {
            if (node == null) {
                return;
            }
            current = node;
            while (current != null) {
                System.out.println(current.data);
                current = current.next;
            }
        }
    
        class Node {
            // 注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问
            int data;// 数据域
            Node next;// 指针域
    
            public Node(int data) {
                this.data = data;
            }
    
            public int getData() {
                return data;
            }
    
            public void setData(int data) {
                this.data = data;
            }
    
            public Node getNext() {
                return next;
            }
    
            public void setNext(Node next) {
                this.next = next;
            }
        }
    
        // 方法:检测单链表是否有环
        public boolean hasCycle(Node head) {
            if (head == null) {
                return false;
            }
            Node first = head;
            Node second = head;
            while (second != null) {
                first = first.next;
                second = second.next.next;
                if (first == second) {
                    return true;
                }
            }
            return false;
    
        }
    
        public static void main(String[] args) {
            LinkListCycle listcycle = new LinkListCycle(); // 向LinkList中添加数据
            for (int i = 0; i < 4; i++) {
                listcycle.add(i);
            }
            listcycle.add(listcycle.head);
            // 将头结点添加到链表当中,于是,单链表就有环了。
            System.out.println(listcycle.hasCycle(listcycle.head));
            
        }
    
    }
  • 相关阅读:
    服务器(Ubuntu 12.04 LTS)上编译基于OpenCV的项目遇到的问题及解决方案
    ubuntu 16.04 LTS 降级安装gcc 4.8
    C#程序中获取电脑硬件配置信息的一种方法
    C#程序将对象保存为json文件的方法
    C#中运用事件实现异步调用
    Redis实现分布式锁 php
    CI框架整合UEditor编辑器上传功能
    PHP给图片加水印具体实现
    检测网站是否被和谐!
    vue的双向绑定和依赖收集
  • 原文地址:https://www.cnblogs.com/guweiwei/p/6863382.html
Copyright © 2011-2022 走看看