Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@shashi1iitk | 398 |

@Suniti Jain | 114 |

@ankish2000nayak | 104 |

@gopalsethlnct | 94 |

@senthil1185 | 90 |

@esmadhan | 90 |

@gyanadipta07 | 89 |

@satwikdash2000 | 85 |

@varunmishra3040 | 83 |

@gamec1634 | 82 |

@pj_1721 | 80 |

Complete Leaderboard |

Given a linked list of size **N**. The task is to complete the function **countNodesinLoop() **that checks whether a given Linked List contains a **loop or not **and if the **loop **is present then** return the count of nodes** in a loop or else **return 0. C **is the position of the node to which the last node is connected. If it is 0 then no loop.

**Example 1:**

**Input:
**N = 10
value[]={25,14,19,33,10,21,39,90,58,45}
C = 4
**Output: **7**
Explanation: **The loop is 45->33. So
length of loop is 33->*10*->21->39->
90->58->*45* = **7. **The number 33 is
connected to the last node to form the
loop because according to the input the
4^{th} node from the beginning(1 based
index) will be connected to the last
node for the loop.

**Example 2:**

**Input:
**N = 2
value[] = {1,0}
C = 1
**Output: **2**
Explanation: **The length of the loop
is 2.

**Your Task:**

The task is to complete the function **countNodesinLoop**() which contains the only argument as reference to head of** linked list **and return the lenght of the loop ( 0 if there is no loop).

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= N <= 500

0 <= C <= N-1

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Find length of Loop

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.