Hello and thanks in advance for answering my query.
This is a piece of code from a tutorial for determining the length of a circular linked list.
def __len__(self):
cur = self.head
count = 0
while cur:
count += 1
cur = cur.next
if cur == self.head:
break
return count
In this code, my query is with 'while cur:'. What is this condition exactly checking? Is it checking if cur = self.head is true? If so, then as cur progresses, in the next iteration, cur will be false and traversing the list should stop. But it goes through till the end.
curis truthy. Initially that's checking ifself.headis truthy, but thencur = cur.next.item.nextwill beNoneif there is nothing afteritem.)