Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@LPMatrix
Last active April 11, 2021 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LPMatrix/ec3cfc327486219891b64cca911526ed to your computer and use it in GitHub Desktop.
Save LPMatrix/ec3cfc327486219891b64cca911526ed to your computer and use it in GitHub Desktop.
# A single node of a singly linked list
class Node:
# constructor
def __init__(self, data = None, next=None):
self.data = data
self.next = next
# A Linked List class with a single head node
class LinkedList:
def __init__(self):
self.head = None
# insertion method for the linked list
def insert(self, data):
newNode = Node(data)
if(self.head):
current = self.head
while(current.next):
current = current.next
current.next = newNode
else:
self.head = newNode
# print method for the linked list
def printLL(self):
current = self.head
while(current):
print(current.data)
current = current.next
# Singly Linked List with insertion and print methods
LL = LinkedList()
LL.insert(3)
LL.insert(4)
LL.insert(5)
LL.printLL()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment