Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Singly-Linked List
class Node:
def __init__(self, key, next):
self.key = key
self.next = next
class List:
def __init__(self, head=None):
self.head = head
self.tail = None
def push_front(self, key):
self.head = Node(key, self.head)
if self.tail == None:
self.tail = self.head
def top_front(self):
return self.head
def pop_front(self):
if head == None:
raise KeyError
self.head = self.head.next
if self.head == None:
self.tail == None
def push_back(self, key):
new_node = Node(key, None)
if self.tail == None:
self.tail = new_node
self.head = self.tail
else:
self.tail.next = new_node
self.tail = new_node
def pop_back(self):
if self.head == None:
raise KeyError
if self.head == self.tail:
self.head = None
self.tail = self.head
else:
p = self.head
while p.next.next != None:
p = p.next
p.next = None
self.tail = p
def find(self, key):
pass
def erase(self, key):
pass
def empty(self)
pass
def add_before(self, node, key):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.