码迷,mamicode.com
首页 > 其他好文 > 详细

11.判断单链表是否有环

时间:2017-05-16 20:50:24      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:完整   头结点   data   bsp   boolean   static   指针   参数表   span   

判断单链表是否有环: 
  这里也是用到两个指针,如果一个链表有环,那么用一个指针去遍历,是永远走不到头的。 
  因此,我们用两个指针去遍历: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));
        
    }

}

 

11.判断单链表是否有环

标签:完整   头结点   data   bsp   boolean   static   指针   参数表   span   

原文地址:http://www.cnblogs.com/guweiwei/p/6863382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!