For some context, the conditional is used in the normal type of algorithm for finding linked list cycles:
From link: http://vijayinterviewquestions.blogspot.com/2007/05/how-would-you-detect-loop-in-linked.html
p=head;
q=head->next;
while(p!=NULL && q!=NULL) {
if(p==q) { //Loop detected! exit(0); }
p=p->next;
q=(q->next)?(q->next->next):q->next;
}
// No loop.
What does the line:
q=(q->next)?(q->next->next):q->next;
get parsed as though? I'm a little confused about operator precedence in this conditional - does q take the value of the left hand side of the ? or of the whole conditional?
that can be expanded to:
if (q->next) {
q = q->next->next;
} else {
q = q->next;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With