【LeetCode 141】Circular linked list, given a linked list, judge whether there is a ring in the linked list.

learning target:

Goal: proficiently use the knowledge learned in Java


Learning Content:

The content of this article: Implemented in Java: Circular Linked List


Title description:

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

If there is a node in the linked list that can be reached again by continuously tracking the next pointer, there is a ring in the linked list. In order to represent the rings in a given linked list, we use the integer pos to indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). If pos is -1, then there is no ring in the linked list. Note: pos is not passed as a parameter, just to identify the actual situation of the linked list.

If there is a ring in the linked list, return true. Otherwise, it returns false.

Example 1:
Insert picture description here

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

Example 2:

Insert picture description here

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

Example 3:

Insert picture description here

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

Problem-solving ideas

To solve this problem, you can use the fast and slow pointer method to define two pointers, two nodes at a time quickly, and one node at a time slowly. If the two pointers meet, it means that the linked list has a ring, and if the two pointers never meet, it means the linked list. No ring

Illustration :

  • first step
    Insert picture description here

  • Second step
    Insert picture description here

  • third step
    Insert picture description here

  • the fourth step
    Insert picture description here

  • the fifth step
    Insert picture description here

At this time, the two hands meet, indicating that the bracelet has a ring

Steps to write code ,

  • Define fast and slow pointers
 ListNode fast=head;//快指针
 ListNode slow=head;//慢指针
  • Loop traversal, fast pointer walks two nodes at a time, slow pointer walks one node at a time, and judges whether they can meet
    while(fast!=null&&fast.next!=null){
    
    
            fast=fast.next.next;//快指针,一次两个节点
            slow=slow.next;//慢指针,一次一个节点
            //判断是否相等
            if(slow==fast){
    
    
                return true;
            }
        }

Implementation code

   public static boolean hasCycle(ListNode head) {
    
    
        ListNode fast=head;//快指针
        ListNode slow=head;//慢指针
        while(fast!=null&&fast.next!=null){
    
    
            fast=fast.next.next;//快指针,一次两个节点
            slow=slow.next;//慢指针,一次一个节点
            //判断是否相等
            if(slow==fast){
    
    
                return true;
            }
        }
        return false;
    }

Guess you like

Origin blog.csdn.net/zhangxxin/article/details/114536779