【LeetCode 142】Circular linked list II, given a linked list, return the first node of the linked list starting to enter the ring. If the linked list has no rings, null is returned.

learning target:

Goal: proficiently use the knowledge learned in Java


Learning Content:

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


Title description:

Given a linked list, return the first node where the linked list starts to enter the ring. If the linked list has no rings, null is returned. 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 that pos is only used to identify the ring, and will not be passed to the function as a parameter.

Note: It is not allowed to modify the given linked list.

Example 1:
Insert picture description here

Input : head = [3,2,0,-4], pos = 1
Output : Return the linked list node with index 1
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 : Return the linked list node with index 0
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 : return null
Explanation : There is no ring in the linked list.

Problem-solving steps:

  • Step 1: Eliminate the case of empty linked list and only one node
        //排除空链表和只有一个节点的情况
        if(head==null||head.next==null){
    
    
            return null;
        }
  • Step 2: Determine whether the linked list has a ring
//通过循环判断链表是否带环
        while(fast!=null&&fast.next!=null){
    
    
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
    
    
                //表示链表带环,结束循环
                break;
            }
        }
  • Step 3: If the linked list has no rings, return empty
   //判断上述while循环结束的原因,只有两个原因
   //第一个原因:如果快指针走到了链表结尾,则返回null
   if(fast==null||fast.next==null){
    
    
       return null;
   }
  • The fourth step: the chain watch with the ring, find the first node of the chain in the ring

If the linked watch has a ring, the node where the fast and slow pointers meet and the head node of the linked list are the same distance from the first node of the linked list into the ring, so it only needs to be traversed synchronously until the first encounter is the first one of the linked list into the ring. Node

  //第二个原因:链表带环
  //如果链表带环,则 快慢指针相遇的结点 和 链表头结点 距离链表入环的第一个结点是相同的
  //所以只需要同步遍历,直到第一次相遇既为链表入环的第一个结点
  while(cur!=fast){
    
    
      cur=cur.next;
      fast=fast.next;
  }
  return fast;

Implementation code:

    public static ListNode detectCycle(ListNode head) {
    
    
        //排除空链表和只有一个节点的情况
        if(head==null||head.next==null){
    
    
            return null;
        }

        ListNode slow=head;//慢指针
        ListNode fast=head;//快指针
        ListNode cur=head;
        //通过循环判断链表是否带环
        while(fast!=null&&fast.next!=null){
    
    
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
    
    
                //表示链表带环,结束循环
                break;
            }
        }
        //判断上述while循环结束的原因,只有两个原因
        //第一个原因:如果快指针走到了链表结尾,则返回null
        if(fast==null||fast.next==null){
    
    
            return null;
        }
        //第二个原因:链表带环
        //如果链表带环,则 快慢指针相遇的结点 和 链表头结点 距离链表入环的第一个结点是相同的
        //所以只需要同步遍历,直到第一次相遇既为链表入环的第一个结点
        while(cur!=fast){
    
    
            cur=cur.next;
            fast=fast.next;
        }
        return fast;
    }

Guess you like

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