双向循环链表的长度_在链表中查找循环长度

双向循环链表的长度

Problem statement:

问题陈述:

Given a linked list, write a program that checks whether the given Linked List contains a loop or not and if the loop is present then return the count of nodes in the loop or else return 0. (this is mainly a functional problem).

给定一个链表,编写一个程序,检查给定的链表是否包含一个循环,如果存在该循环,则返回循环中的节点数,否则返回0。(这主要是一个功能问题)。

Example:

例:

linked list loop

Loop in the linked list

在链表中循环

There is a loop in the above linked list.
As, 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 3 → 4 → 5 → 6 → 7 → 8 → 3...so on.

上面的链接列表中有一个循环。
例如, 1→2→3→4→5→6→7→8→3→4→5→6→7→8→3 ...

Thus, there is a loop between 3 and 8 and the length of loop is 6.

因此, 在3和8之间存在一个循环,循环的长度为6

Solution:

解:

The above problem has two sections:

上面的问题分为两部分:

  1. Detecting loop in the linked list

    在链表中检测循环

  2. Finding length of loop if loop exists

    如果存在循环,则查找循环的长度

We declare two pointers: one slow pointer and another fast pointer. The slow pointer moves slowly, only one step at each iteration. The fast pointer moves fast, two steps at each iteration. Slow pointer and fast pointer are initially set to the head & head->next respectively.

我们声明了两个指针:一个慢速指针和另一个快速指针。 慢速指针缓慢移动,每次迭代仅一步。 快速指针快速移动,每次迭代两个步骤。 慢速指针和快速指针最初分别设置为head&head-> next。

If there is a loop then the fast pointer and slow pointer will point to a similar node after finite no of iteration
Else
The fast pointer will reach NULL.

如果存在循环,则在有限的迭代次数后,快速指针和慢速指针将指向相似的节点
其他
快速指针将达到NULL。

Let’s solve the above example using this algorithm...

让我们使用该算法解决以上示例...

    Initially both points to head node...

    At iteration 0:
    slow points to 2 (head->next )
    fast points to 3  (head->next->next)
    
    At iteration 1:
    slow points to 3 (only one traverse one node)
    fast points to 5(traverse two nodes)
    
    At iteration 2: 
    slow points to 4 (only one traverse one node)
    fast points to 7  (traverse two nodes)
    
    At iteration 3: 
    slow points to 5 (only one traverse one node)
    fast points to 3 (traverse two nodes)
    
    At iteration 4: 
    slow points to 6 (only one traverse one node)
    fast points to 5  (traverse two nodes)
    
    At iteration 5: 
    slow points to 7 (only one traverse one node)
    fast points to 7 (traverse two nodes)

    both points to same node, loop detected

In case there’s no loop, let’s consider these case too...

万一没有循环,让我们也考虑这些情况...

Such an example is any single linked list terminated by NULL, 1 → 2 → 3 → 4 → 5 → 6 → NULL

这样的示例是任何以NULL终止的单个链表,1→2→3→4→5→6→NULL

In this case the fast pointer reaches the NULL fast and the program terminates detecting no loop.

在这种情况下,快速指针快速到达NULL,并且程序终止,并且没有检测到循环。

查找循环长度 (Finding length of loop)

    If there is no loop then length of loop is 0.
    Else
    Slow pointer is already pointing to the node at which we detected the loop.
    Now, set counter=1  & temp=slow->next;
    while (temp!=slow){
        temp=temp->next;
        counter++;
    } 
    Return counter; // length of loop

C ++实现在链表中查找循环长度 (C++ Implementation to find length of loop in a linked list)

#include <bits/stdc++.h>
using namespace std;

class Node{//linked list node
	public:
		int data;
		Node* next;
};

int loopLength(Node *head){
	Node* slow=head,*fast=head; //initialize
	while(slow && fast && fast->next){
		slow=slow->next; //slow pointer moves slowly
		fast=fast->next->next; //fast pointer moves fast
		if(fast==slow){//there is loop
			//count loop length
			Node* temp=slow;
			temp=temp->next;
			int count=1;
			while(temp!=slow){
				count++;
				temp=temp->next;
			}
			return count; //return loop length
		}
	}
	return 0;//if there is no loop
}


Node *newNode(int k){ //defining new node
	Node *temp = (Node*)malloc(sizeof(Node)); 
	temp->data = k; 
	temp->next = NULL; 
	return temp; 
} 

/* Driver program to test above function*/
int main() { 
	cout<<"linked list is built like the example\n";
	cout<<"1->2->3->4->5->6->7->8->3->4->....\n";
	Node *head = newNode(1); 
	head->next = newNode(2); 
	head->next->next = newNode(3); 
	head->next->next->next = newNode(4); 
	head->next->next->next->next = newNode(5);
	head->next->next->next->next->next = newNode(6);
	head->next->next->next->next->next->next = newNode(7);
	head->next->next->next->next->next->next->next = newNode(8);

	/* Create the loop  */
	//the next pinter of node having value 8 coonected to node having value 3
	head->next->next->next->next->next->next->next->next = head->next->next ; 

	printf("loop length: %d \n", loopLength(head)); 

	return 0; 
} 

Output

输出量

linked list is built like the example
1->2->3->4->5->6->7->8->3->4->....
loop length: 6


翻译自: https://www.includehelp.com/icp/find-length-of-loop-in-a-linked-list.aspx

双向循环链表的长度

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值