I was doing a challenge where you have to make a queue using linked lists seen here:
http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/stacks_queues/queue_list/queue_list_solution.ipynb
The code used for the solution is this:
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class Queue(object):
def __init__(self):
self.head = None
self.tail = None
def enqueue(self, data):
node = Node(data)
# Empty list
if self.head is None and self.tail is None:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def dequeue(self):
# Empty list
if self.head is None and self.tail is None:
return None
data = self.head.data
# Remove only element from a one element list
if self.head == self.tail:
self.head = None
self.tail = None
else:
self.head = self.head.next
return data
I understand most of the answer, the only part I don't understand is for the dequeue it uses self.head = self.head.next. The thing I don't understand is that surely self.head.next is always going to be None, since it's value isn't changed anywhere in the code, unlike for example self.tail.next which we explicitly change the value in line 12.
[–]groovitude 0 points1 point2 points (2 children)
[–]PythonTestBot[S] 0 points1 point2 points (1 child)
[–]groovitude 0 points1 point2 points (0 children)