How to count the number of nodes in a linked list without traversing it?
问题 I have been asked in an interview how to count the number of nodes in a linked list without traversing the list? Is there any way to achieve this? 回答1: The only way I can think of is to add a counter of the number of nodes which is incremented each time the add or insert methods are invoked, and decremented when delete is invoked. You cannot make assumptions about memory occupied because, being a linked list, you cannot guarantee that all nodes will be in the same memory block (indeed, this