COSC 2306 - Singly Linked List Visualizer
newnode.next to point to the current head.head to point to the newnode.head.current.next == None.current.next to the newnode.head = head.next.current and previous to track nodes.head = current.next.previous.next = current.next.
class Node:
def __init__(self, initdata):
self.data = initdata
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(self, value):
newnode = Node(value)
newnode.next = self.head
self.head = newnode
def append(self, item):
current = self.head
if current == None:
self.head = Node(item)
else:
while current.next != None:
current = current.next
current.next = Node(item)
def pop(self):
if self.head == None:
return None
self.head = self.head.next
def remove(self, item):
current = self.head
previous = None
found = False
while not found and current != None:
if current.data == item:
found = True
else:
previous = current
current = current.next
if current == None:
return # item not found
if previous == None:
self.head = current.next
else:
previous.next = current.next