快慢指针-java语言版

689 阅读3分钟

快慢指针指的是定义两个指针,这两个指针的移动速度一块一慢,以此来制造出自己想要的差值,这个差值可以让我们找到链表上相应的结点。一般情况下,快指针的移动步长为慢指针的两倍

-- 中间值问题:

利用快慢指针,我们把一个链表看成一个跑道,假设a的速度是b的两倍,那么当a跑完全程后,b刚好跑一半,以 此来
达到找到中间节点的目的。

-- 如下图,最开始,slow与fast指针都指向链表第一个节点,然后slow每次移动一个指针,fast每次移动两个指针。

中间值1.png

中间值2.png

中间值3.png

中间值4.png

-- 代码:

public class Test {
    public static void main(String[] args) throws Exception {
        // 创建新节点
        Node<String> first = new Node<String>("aa", null);
        Node<String> second = new Node<String>("bb", null);
        Node<String> third = new Node<String>("cc", null);
        Node<String> fourth = new Node<String>("dd", null);
        Node<String> fifth = new Node<String>("ee", null);
        Node<String> six = new Node<String>("ff", null);
        Node<String> seven = new Node<String>("gg", null);

        // 完成结点的指向
        first.next = second;
        second.next = third;
        third.next = fourth;
        fourth.next = fifth;
        fifth.next = six;
        six.next = seven;

        // 查找中间值
        String mid = getMid(first);
        System.out.println("中间值为:" + mid);
    }

    /**
     *
     * @param first 首结点
     * @return  返回中间值
     */
    public static String getMid(Node<String> first) {
        // 定义快慢指针刚开始,它们都在指向头结点
        Node<String> slow = first;
        Node<String> fast = first;
        // 快慢指针开始移动
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        // 快指针到达末尾后,慢指针就是中间值
        return slow.item;
    }

    //结点类
    private static class Node<T> {
        //存储数据
        T item;
        //下一个结点
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
}

-- 运行效果图:

中间值运行效果图.png


-- 是否有环问题:

是否有环.png

   使用快慢指针的思想,还是把链表比作一条跑道,链表中有环,那么这条跑道就是一条圆环跑道,
在一条圆环跑道 中,两个人有速度差,那么迟早两个人会相遇,只要相遇那么就说明有环。

环1.png

环2.png

环3.png

环4.png

环5.png

环6.png -- 代码:

public class Test {
    public static void main(String[] args) throws Exception {
        // 创建新节点
        Node<String> first = new Node<String>("aa", null);
        Node<String> second = new Node<String>("bb", null);
        Node<String> third = new Node<String>("cc", null);
        Node<String> fourth = new Node<String>("dd", null);
        Node<String> fifth = new Node<String>("ee", null);
        Node<String> six = new Node<String>("ff", null);
        Node<String> seven = new Node<String>("gg", null);

        //完成结点之间的指向
        first.next = second;
        second.next = third;
        third.next = fourth;
        fourth.next = fifth;
        fifth.next = six;
        six.next = seven;
        //产生环
        seven.next = third;
        //判断链表是否有环
        boolean circle = isCircle(first);
        System.out.println(circle);
    }

    /**
     *
     * @param first 首结点
     * @return  返回中间值
     */
    public static boolean isCircle(Node<String> first) {
        // 定义快慢指针刚开始,它们都在指向头结点
        Node<String> slow = first;
        Node<String> fast = first;
        // 快慢指针开始移动
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast.equals(slow)) {
                // 如果它们相遇就证明有环、返回true
                return true;
            }
        }
        // 没有环返回false
        return false;
    }

    //结点类
    private static class Node<T> {
        //存储数据
        T item;
        //下一个结点
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
}

-- 运行结果

环运行结果.png


-- 环入口问题:

    当快慢指针相遇时,我们可以判断到链表中有环,这时重新设定一个新指针指向链表的起点,
且步长与慢指针一样 为1,则慢指针与“新”指针相遇的地方就是环的入口。证明这一结论牵涉
到数论的知识,这里略,只讲实现。

入口1.png

环2.png

环3.png

-- 代码:

public class Test {
    public static void main(String[] args) throws Exception {
        // 创建新节点
        Node<String> first = new Node<String>("aa", null);
        Node<String> second = new Node<String>("bb", null);
        Node<String> third = new Node<String>("cc", null);
        Node<String> fourth = new Node<String>("dd", null);
        Node<String> fifth = new Node<String>("ee", null);
        Node<String> six = new Node<String>("ff", null);
        Node<String> seven = new Node<String>("gg", null);

        //完成结点之间的指向
        first.next = second;
        second.next = third;
        third.next = fourth;
        fourth.next = fifth;
        fifth.next = six;
        six.next = seven;
        //产生环
        seven.next = third;
        //查找环的入口结点
        Node<String> entrance = getEntrance(first);
        System.out.println("first链表中环的入口结点元素为:"+entrance.item);
    }

    /**
     *
     * @param first 首结点
     * @return  返回中间值
     */
    public static Node getEntrance(Node<String> first) {
        // 定义快慢指针刚开始它们都在指向头结点
        Node<String> slow = first;
        Node<String> fast = first;
        // 当快慢指针相遇,则让这个移动用于查找环入口
        Node<String> temp = null;
        // 快慢指针开始移动
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast.equals(slow)) {
                // 如果它们相遇就证明有环、返回true
                temp = first;
                continue;
            }

            if(temp != null) {
                temp = temp.next;
                if(temp.equals(slow)) {
                    return temp;
                }
            }
        }
        // 没有找到环入口
        return null;
    }

    //结点类
    private static class Node<T> {
        //存储数据
        T item;
        //下一个结点
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
}

-- 运行效果图:

环入口运行效果.png

@ 以上内容属于个人笔记