Last active
April 11, 2021 15:25
-
-
Save LPMatrix/ec3cfc327486219891b64cca911526ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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