LeetCode 150 classic interview questions -- circular linked list (simple)

1. Topic

Given the head node of a linked list head, determine whether there is a ring in the linked list.

If there is a node in the linked list that can be nextreached again by continuously tracking the pointer, then there is a cycle in the linked list. In order to represent the ring in the given linked list, the evaluation system internally uses an integer posto indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). Note: posNot passed as a parameter  . Just to identify the actual situation of the linked list.

 Returns if there is a cycle in the linked listtrue . Otherwise, return false.

2. Examples

Example 1:

 Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a ring in the linked list whose tail is connected to the second node.

 Example 2:

 Input: head = [1,2], pos = 0
Output: true
Explanation: There is a ring in the linked list whose tail is connected to the first node.

 Example 3:

 Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list

Tip: Structures in Linked Lists

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

3. Ideas

Fast and slow pointer:

The fast and slow pointer algorithm can be used for this kind of loop problem or chasing problem. Because it is circular, unless the fast pointer finds null first, the fast and slow pointer must meet, and the encounter of the two means the cycle of the linked list , because in general, the fast pointer moves fast, and the slow pointer moves slowly, but when the two speeds are obviously different, they meet, which means that the linked list is circular

Hash collection:

Since the end of the cycle is to check whether there is an overlapped address, you only need to save the node address of the linked list when traversing down, and if the next node address already exists in the hash table during the next traversal, then means the linked list is circular

4. Code

LeetCode code

Fast and slow pointer:

public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false; // 链表为空或只有一个节点,必然无环
        }
        
        ListNode slowIndex = head;
        ListNode fastIndex = head;
        
        while (fastIndex != null && fastIndex.next != null) {
            slowIndex = slowIndex.next; // 慢指针每次移动一个节点
            fastIndex = fastIndex.next.next; // 快指针每次移动两个节点
            
            if (slowIndex == fastIndex) {
                return true; // 快慢指针相遇,存在环
            }
        }
        
        return false; // 快指针到达链表尾部,无环
    }
}

 Time complexity O(n), space complexity O(1)

 Hash collection:

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

 Time complexity O(n), space complexity O(n) 


Will it? Try to challenge the next question! ♪(^∀^●)ノシ(●´∀`)♪

LeetCode 150 classic interview questions--merging two ordered linked lists (simple)_Alphamilk's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/dogxixi/article/details/132320935