One (admittedly stupid) way to do this is to use the behavior of short trailing logical operators && and || . For example, you can do this:
AddTail(tNode* pNode){ pTail || (pHead = pNode); !pTail || (pTail->pNext = pNode); pTail = pNode; pTail->pNext = NULL; }
This works because if ptail is NULL, the first part of the first statement will evaluate to false, forcing the second half of the || . If it is not equal to zero, the second half of the instruction will not be evaluated. Similar logic works for the following statement.
However, this is a very stupid interview question, and I honestly don't know what they are aiming for. Personally, I would question the wisdom of someone trying to gauge your ability to write such code.
Hope this helps!
source share