链表——单链表的环问题

package study;
  /*
   * 单链表的环
*/
class Outer5 {
    class Entry {
        int data;
        Entry next;

        public Entry() {
            data = 0;
            next = null;
        }

        public Entry(int data) {
            this.data = data;
            next = null;
        }
    }

    private Entry head = new Entry();
    /**
     * 头插法
     * @param val 需要插入的数据
     */
    public void inserthead(int val) {
        Entry entry = new Entry(val);
        entry.next = head.next;
        head.next = entry;

    }
    /*
     * 创建环
    */
    public void cjh() {
        Entry cur = head;
        while( cur.next != null) {
            cur = cur.next;
        }
        cur.next = head.next.next.next;
    }
    /*
     * 判断环,设置两个引用,其中一个引用一次走两步,一个一次走一步,如果两个引用在某一位置相遇,则说明有环
    */
    public boolean pdh() {
        Entry first = head;
        Entry last = head;
        while(first != null && first.next != null) {
            first = first.next.next;
            last = last.next;
            if(first == last) {
                return true;
            }
        }
        return false;
    }
    /*
     * 获得环的入口节点,因为快引用是慢引用的两倍,可以得到在相遇的时候,将某一个引用放到链表的头,
     * 然后两个指针都开始一步一步的走,当他们再次相遇的时候就是环的入口点了。
    */
    public int hdrk() {
        if(!pdh()) {
            return -1;
        }
        Entry first = head;
        Entry last = head;
        while(first != null && first.next != null) {
            first = first.next.next;
            last = last.next;
            if(first == last) {
                break;
            }
        }
        first = head;
        while(first != last) {
            first = first.next;
            last = last.next;
        }
        return first.data;
    }

    /*
     * 求环的长度,方法1
    */
    public int getLoopLength(){   
        if(!pdh()) {
             return -1;
        }
        boolean tag = false;
        Entry fast = head;
        Entry slow = head;
        int count=0;
        while(fast!=null&&fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast==slow&&tag==true)
                break;
            if(fast==slow&&tag ==false)
                tag=true;
            if(tag=true)
                count++;    
        }
        return count;
    }
    /*
     * 求环的长度,方法2
    */
    public int hecd() {
        if(!pdh()) {
            return -1;
        }
        Entry first = head;
        Entry last = head;
        while(first != null && first.next != null) {
            first = first.next.next;
            last = last.next;
            if(first == last) {
                break;
            }
        }
        last = last.next;
        int len = 1;
        while(first != last ) {
            last = last.next;
            len++;
        }
        return len;
    }

    //
    public void show() {
        Entry cur = head;
        while(cur.next != null) {

            cur = cur.next;
            System.out.print(cur.data+" ");
        }
        System.out.println();
    }
}
public class Lianxi1 {

    public static void main(String[] args) {
        Outer5 s= new Outer5();
        s.inserthead(25);
        s.inserthead(35);
        s.inserthead(45);
        s.inserthead(55);
        s.inserthead(65);
        s.inserthead(75);
        s.inserthead(85);
        s.inserthead(95);
        s.show();
        s.cjh();
        System.out.println(s.pdh());
        System.out.println(s.hdrk());
        System.out.println(s.hecd());
    }

}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq2899349953/article/details/80231905
今日推荐