Home > Algorithms, General > Find whether LinkedList is cyclic

Find whether LinkedList is cyclic


I was browsing through and came across this algorithm some where, which checks whether linked list is cyclic or not. This is to brush up my concepts on datastructures πŸ™‚

Start with 2 pointers at the first node of the linked list

Loop infinitely
  if fastPointer reach Null
     return 'No Cycle'
  if fastPointer moves on to slowPointer
     return 'Cycle'
  move slowPointer by one Node
  move fastPointer by two Nodes
endLoop
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: