Ch2-5: find the beginning of loop in a circular linked list---two solutions

Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.

DEFINITION

Circular 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 linked list.

EXAMPLE

Input: A -> B -> C -> D -> E -> C [the same C as earlier]

Output: C


这题有2中做法,书上的那个还没仔细看(2个指针,一个+2,一个+1,如果重复了,就说明有环。但是怎么找到环的头节点呢?关键是:

在重复的时候,把slow重新指向head,同时把fast的速度改为一,那么他们再次相遇的时候就是环路的头了)公式请参考hawstein。

I thinks the 2nd idea is simple as similar to early solution: use a hashmap to have key(node addr), if the addr is duplicate, it means there is a loop.

#include 
   
   
    
    
#include 
    
    
using namespace std;

typedef struct node{
    int data;
    node *next;
}node;

// initialize a linkedlist with a loop,
// the n is the total nodes, m is the starting 
// node of the loop, still a singly linked list 
node* init(int *a, int n, int m){  
    node *head, *p, *q;
    for(int i=0; i
     
     
      
      data = a[i];
        if(i==m) q = nd;
        if(i==0){
            head = p = nd;
            continue;
        }
        p->next = nd;
        p = nd;
    }
    p->next = q;
    return head;
}

// seems like a math contest idea
node* loopstart0(node *head){   // see the explaination
    node *fast = head, *slow=head;
    while(fast || fast->next){
        fast = fast->next->next;
        slow = slow->next;
        if(fast==slow)
            break;
    }
    if(!fast || !fast->next) return NULL;
    slow = head;
    while(fast!=slow){
        fast = fast->next;
        slow = slow->next;
    }
    return fast;
}

// method 2: simple and easy to understand
map
      
      
       
        hashmap;

node* loopstart1(node *head){   
    while(head){
        if(hashmap[head]) return head;
        else{
            hashmap[head] = true;
            head = head->next;
        }
    }
    return head;
}
int main(){
    int n =6, m =2 ;// m
       
       
         data< 
         
       
      
      
     
     
   
   

输出结果:

Executing the program....
$demo 
3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值