PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

Detect if there is a cycle in a linked list

(No Ratings Yet)

November 4, 2012 by asinghal 1 Comment

How can you find if a given linked list has a cycle in it?

Note: Linked list is singly linked list (there is only next pointer no previous pointer).
See Solution: Detect if there is a cycle in the linked listHide Solution

This can also be solved by using two pointers as we saw in Find nth element from end of linked list and Find middle element of linked list. Take two pointers pointer1 and pointer2, both pointing to head of the linked list initially, now increment pointer1 by one node and pointer2 by two nodes, If at any point of time pointer1 and pointer2 becomes equal then there is a cycle in the linked list, else pointer2 will reach end of the list and will become null eventually.

Code in C

bool isCyclePresent(Node* node){
	Node* pointer1 = node;
	Node* pointer2 = node;
	
	while(pointer2 != null && pointer2->next!=null){
		pointer1 = pointer1->next;
		pointer2 = pointer2->next->next;
		if(pointer1 == pointer2)
			return true;
	}
	return false;
}
  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Next Puzzle
Reverse a link list without using another

Checkout more Interview Questions Tags: Interview, Linked List, medium, Solved Puzzles

Comments

  1. sonu sehrawat says

    January 26, 2013 at 3:02 pm

    In while loop,instead of “&&” ,we should hv used ” || “.

    Reply

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • Manage Your Energey Solution: Google codejam 2013 Round 1A
  • Tic-Tac-Toe-Tomek Solution: Google codejam 2013 Qual Round
  • 3 SUM problem
  • Cake Cutting solution: Facebook hacker cup 2013 Round 2
  • Minesweeper Master Solution – Google Code jam 2014
  • Find the Min solution: Facebook hacker Cup 2013 Qual Round
  • Charging Chaos Solution – Google CodeJam
  • Consonants Solution: Google codejam 2013 Round 1C
  • Objective Questions set C/C++ – Part 6
  • Output of the recursive program

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▲
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous