Results 1 to 2 of 2

Thread: How can a cycle be detected in a singly linked list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Geek_Guest
    Guest

    How can a cycle be detected in a singly linked list

    How can a cycle be detected in a singly linked list?

    Question asked by visitor mahesh dixit


  2. #2
    Junior Member
    Join Date
    Jul 2007
    Answers
    5

    Smile Re: How can a cycle be detected in a singly linked list

    Quote Originally Posted by Geek_Guest View Post
    How can a cycle be detected in a singly linked list?

    Question asked by visitor mahesh dixit
    hi,
    i think this logic shud work..chek it..
    Take 2 pointers, p and q.
    p = p -> next(increment p by 1)
    q = q-> next -> next (increment q by 2)
    can put this in while loop condition (p!= q)
    if(p == NULL || q == NULL)
    {
    no loop is encountered
    }
    if(p == q)
    {
    the singly linked list has a loop
    }
    Hope i answered u..plz let me know if i am right


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact