Detecting Length Of Loop In A Linked List

easy
1. You are given a linked list with N nodes.
  2. You are also given a number x which means last node is connected with
     xth node of linked list. Therefore, there exists a loop.
  3. If x is 0, it means last node is connected to nothing. Therefore, no loop. 
  4. You have to write a function that counts the number of nodes in the loop.
  5. Linked list can contain self loop.
  6. display is a utility function which displays the contents of Linked List,
     feel free to use it for debugging purposes.
  7. main takes input from the users and creates the Linked List. You can use
     display to know its contents.
  8. This is a functional problem. 
  9. You should code only the countNodesInLoop function. It takes as input the
     head of the linked list and returns the count of nodes in loop.
  10. Don't change the code of Node, main and display.

Input Format

First line takes N, the number of elements in the list. Next line takes input N space separated numbers reperesenting elements of the linked list. Next line takes input x. Input is handled for you.

Output Format

Count of nodes in the loop. Output is handled for you.

Constraints

1 <= N <= 300
  0 <= x <= N

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
10
25 14 19 33 10 21 39 90 58 45
8
Output
2
Previous
Fun With Asteroids
Next
Counting the number of reversals

Related Questions