392 views
1 votes
1 votes

1 Answer

0 votes
0 votes

You are correct.. for `p` to ever be NULL, there needs to be an empty list and q!=NULL already checks for that

BUT

When the given list is empty (head = NULL), NULL->next has UNDEFINED behaviour, this means that when the 2nd line of code


Q = head→next               //becomes

Q = NULL→next .

Then this code will either throw error (Compiler) or Segmentation fault or (if it is/isn't run in "X" language) it can (it shouldn't) still assign a value to Q .

 If it does assign a value then the while code will execute and if the code is 

while ( q!=NULL )

 This can lead the loop to execute forever just because a "series" of UNDEFINED behaviour "can" occour. I am strongly  emphasising on the words "series" and "can".

Hence the accurate code is infact

while( (p!=NULL) && (q!=NULL) )

i.e. option C

No related questions found