-1

i want to know that why we use two conditions i am confused.. while head and head->next are not equal to null which means head and head's next will point to null ho is that possible

int detectCycle(Node *& head)
{
   Node * fast = head;
   Node * slow = head;
   while(fast!=nullptr && fast->next!=nullptr)   // i am confused in these two conditions
   {
     slow = slow->next;
     fast = fast->next->next;
     if(fast == slow)
        return 1;
   }
    return false;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Why do you think "head and head's next will point to null" if they are **not equal** to `nullptr`? – Drew Dormann Nov 02 '22 at 19:21
  • You need to check that `fast` is not `nullptr` before you try to dereference it to get to `fast->next`. – Jesper Juhl Nov 02 '22 at 19:24
  • I'm not following what it is that's confusing you. Are you asking about the meaning of `!=nullptr`? Are you asking about why there are two tests instead of just one? Something else? – John Bollinger Nov 02 '22 at 19:25
  • 2
    If you just did `if (fast->next)` and `fast` was `nullptr`, what would you expect to happen? (The answer is [UB](https://en.cppreference.com/w/cpp/language/ub)). – Jesper Juhl Nov 02 '22 at 19:26
  • `return 1;` - don't do that in a function returning `bool`. `return true;` instead. It does the same thing, but don't be weird when writing code - you have to read it again in the future. – Jesper Juhl Nov 02 '22 at 19:30
  • @JohnBollinger i am asking whether head and head->next cant point to null ? if yes how? – Athar Mujtaba Wani Nov 02 '22 at 19:30
  • "i am asking whether head and head->next cant point to null ?" - How can we know? You didn't post enough code. We can't see how your function is called. Someone could easily be calling it with `nullptr` as an argument. – Jesper Juhl Nov 02 '22 at 19:32
  • `head` and `head->next` (or `fast` and `fast->next`) cannot *both* be null at the same time, because if `head` is null then `head->next` is undefined. But I see no reason why one or the other couldn't be null. If you think you see such a reason then maybe it would help to try to explain that to us. Or even to your rubber duck. – John Bollinger Nov 02 '22 at 19:46

2 Answers2

1

This condition

while(fast!=nullptr && fast->next!=nullptr)

means that the current pointer fast and its data member next that is a pointer to the next node are not null pointers. If so you may write

 fast = fast->next->next;

And the function should be declared like

bool detectCycle(Node *& head)
^^^^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

while head and head->next are not equal to null which means head and head's next will point to null ho is that possible

From the code presented, I guess you mean fast where you wrote head.

If fast is null then fast!=nullptr evaluates to false. In that case it would produce undefined behavior to evaluate fast->next, but that is not an issue because the && operator has short-circuiting behavior. That is, if the left operand of an && operator evaluates to false, then the right operand is not evaluated.

Thus the while condition is safe to evaluate, and it evaluates to true if and only if neither fast nor fast->next is a null pointer.

Supposing that head is a pointer to the head node of a linked list, the code seems to assume that the last node can be recognized by having a next pointer whose value is null. This is a very common paradigm, so it is absolutely reasonable to think that as the fast pointer is advanced through the list (two links at a time), it will eventually become the case that either it or its successor will be the last node, and therefore have a null next pointer.

If that happens, then the list is proven not to loop. If the list does loop, however, then it will eventually be the case that slow, which advances only one link at a time instead of two, will point to the same node that fast does. When that is detected, the list is known to loop.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157