链表中的环

判断链表是否有环,定义指针一快(走2部)一慢(走1部),相遇即有环。

bool isCircle(listNode* head){
        if(NULL == head)
           return false;
        listNode* slowNode = head->next;
        if(NULL == slowNode)
           return false;
        listNode* fastNode = head->next;
        while(fast != NULL && slow != NULL){
                 if(fast == slow)
                    return true;

                 slowNode = slowNode->next;
                 fastNode = fastNode->next;
                 if(fastNode != NULL)                   //快指针时刻检查,是否为空
                    fastNode = fastNode->next;
        }
        return false;
}

两个指针,一快一慢,有环,则相遇必在环内,找出相遇节点

listNode* meetingNode(listNode* head){
        if(NULL == head)
           return NULL;
        listNode* slowNode = head->next;
        if(NULL == slowNode)
           return NULL;
        listNode* fastNode = head->next;
        while(fast != NULL && slow != NULL){
                 if(fast == slow)
                    return fast;

                 slowNode = slowNode->next;
                 fastNode = fastNode->next;
                 if(fastNode != NULL)                   //快指针时刻检查,是否为空
                    fastNode = fastNode->next;
        }
        return NULL;
}

接下来,就可以统计环中节点个数,找出环的入口节点

设节点个数为n,快指针先走n步,然后快慢指针一起一步一步走,相遇节点即环入口节点。

时间: 2024-08-10 07:25:53

链表中的环的相关文章

求有环单链表中的环长、环起点、链表长

1.判断单链表是否有环 使用两个slow, fast指针从头开始扫描链表.指针slow 每次走1步,指针fast每次走2步.如果存在环,则指针slow.fast会相遇:如果不存在环,指针fast遇到NULL退出. 就是所谓的追击相遇问题: 2.求有环单链表的环长 在环上相遇后,记录第一次相遇点为Pos,之后指针slow继续每次走1步,fast每次走2步.在下次相遇的时候fast比slow正好又多走了一圈,也就是多走的距离等于环长. 设从第一次相遇到第二次相遇,设slow走了len步,则fast走

[CareerCup] 2.6 Linked List Cycle 单链表中的环

2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the

第九十题(1.不开辟暂时空间交换 2.删除串中指定字符 3.推断链表中存在环)

1.不开辟用于交换数据的暂时空间,怎样完毕字符串的逆序 2.删除串中指定的字符 3.推断单链表中是否存在环 分析和代码: 1,不开辟用于交换的暂时空间,能够用异或交换.或者用字符串的'\0'位置的空间(打个擦边球,使用已有空间.不算开辟). void switch1(char* str) //使用异或交换 { int len = strlen(str); for (int i = 0; i < len / 2; i++) str[i] ^= str[len - i - 1] ^= str[i]

[LeetCode] Linked List Cycle II 单链表中的环之二

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参见我之前的一篇文章 (http://www.cnblogs.com/grandyang/p/4137187.html). 还是要设

一个链表中包含环,请找出该链表的环的入口结点

方法一.用HashSet来解决 1 public ListNode EntryNodeOfLoop(ListNode pHead){ 2 HashSet<ListNode> hs = new HashSet<ListNode>(); 3 while(pHead!=null){ 4 if(!hs.add(pHead))//如果包含了,那么这个就是入口结点 5 return pHead; 6 pHead = pHead.next; 7 } 8 return null; 9 } 方法二.

[LeetCode] Linked List Cycle 单链表中的环

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 开始以为这道题只需要注意不使用额外空间即可,于是写了个时间复杂度为O(n^2)暴力搜索算法,如下: /** * Dumped, Time Limit Exceeded */ class Solution { public: bool hasCycle(ListNode *hea

[LintCode] Linked List Cycle 单链表中的环

Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail connects to node index 1, return true Challenge Follow up:Can you solve it without using extra space? s

java 链表中是否有环

public class 链表中是否有环{    class ListNode    {        ListNode next;        int val; ListNode(int x)        {            this.val = x;            this.next = null;        }    } public boolean hasCycle(ListNode head)    {        if (head == null)      

LeetCode -- 判断链表中是否有环

思路: 使用两个节点,slow和fast,分别行进1步和2步.如果有相交的情况,slow和fast必然相遇:如果没有相交的情况,那么slow或fast必然有一个为null 相遇时有两种可能:1. 只是节点相交的情况,即:slow == fast但是 slow.next != fast.next2. 链表中存在环,即slow == fast 而且 slow.next == next 实现代码: public bool HasCycle(ListNode head) { // - for null