class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, value):
if self.head is None:
self.head = Node(value)
return
# Move to the tail (the last node)
node = self.head
while node.next:
node = node.next
node.next = Node(value)
return
I'm a little confused on how the while loops statement works in this context. While loops are suppose to work as long as the condition is true. I am not sure how the while loop condition would return true or false in this context can someone please explain. Thank you!
node.next
evaluates to a value, and that value is then evaluated as a boolean.
Specifically, if node.next = None
, bool(None) == False
and the loop breaks. Otherwise bool(<Node object>) == True
and the loop proceeds.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.