【leetcode】环形链表及拓展

版权声明:转载请注明 https://blog.csdn.net/qq_33591903/article/details/82966751

                                              环形链表及拓展

一、要求

给定一个链表,判断链表中是否有环。

进阶:
你能否不使用额外空间解决此题?

节点类:

 class ListNode {
        public int val;
        public ListNode next;

        ListNode(int val) {
            this.val = val;
            this.next = null;
        }
    }

二、解法

(1)使用额外空间来判断链表中是否有环

思路:遍历整个链表,将每一次遍历的节点存入Set中,利用Set存入相同元素返回false的特性,判断链表中是否有环。

 public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        while (head != null) {
            boolean result = set.add(head);
            if (!result) {
                return true;
            }
            head = head.next;
        }
        return false;
    }

由于遍历,导致时间复杂度为O(n),由于使用了Set集合,空间复杂度为O(n)。


(2)使用快慢指针。

思路:快慢指针都从头节点开始,快指针一次走两步,慢指针一次,如果慢指针能够追赶上快指针,则证明链表中有环。

 public boolean hasCylce2(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            //如果慢指针追赶上快指针的话,则说明有环
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

三、拓展

拓展问题一:

如果链表有环,找出环的入口节点。

思路:快慢指针的相遇点到环入口的距离等于头节点到环入口的距离,那么在头节点和相遇点各设一个相同步伐的指针,他们相遇的那个节点就是环入口。

public ListNode getEntrance(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        boolean isCycle = false;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            //如果慢指针追赶上快指针的话,则说明有环
            if (fast == slow) {
                isCycle = true;
                break;
            }
        }

        if (isCycle) {
            slow = head;
            while (slow != fast) {
                slow = slow.next;
                fast = fast.next;
            }
            return slow;
        }
        return null;
    }

拓展问题二:

若链表有环,求出环的长度。

扫描二维码关注公众号,回复: 3530384 查看本文章

思路:若链表有环,得到环入口,然后让指针指向环入口,指针遍历完重新回到环入口的路程即环的长度。

 public int getCylceLength(ListNode head) {
        int length = 0;
        ListNode cycleNode = getEntrance(head);
        if (cycleNode != null) {
            ListNode temp = cycleNode;
            while (true) {
                temp = temp.next;
                length++;
                if (temp == cycleNode) {
                    break;
                }
            }
        }
        return length;
    }

猜你喜欢

转载自blog.csdn.net/qq_33591903/article/details/82966751
今日推荐