0% found this document useful (0 votes)
4 views

Python Code

Uploaded by

seralathan s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Code

Uploaded by

seralathan s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class ListNode:

def __init__(self, val):


self.val = val
self.next = None

class Queue:
# Implementing this with dummy nodes would be easier!
def __init__(self):
self.left = self.right = None

def enqueue(self, val):


newNode = ListNode(val)

# Queue is non-empty
if self.right:
self.right.next = newNode
self.right = self.right.next
# Queue is empty
else:
self.left = self.right = newNode

def dequeue(self):
# Queue is empty
if not self.left:
return None

# Remove left node and return value


val = self.left.val
self.left = self.left.next
return val

def print(self):
cur = self.left
while cur:
print(cur.val, ' -> ', end ="")
cur = cur.next
print() # new line

You might also like