-1
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!

Nick
  • 624
  • 8
  • 24
  • 1
    See https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false – Alex Hall Apr 15 '20 at 16:31

1 Answers1

2

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81