Skip to content

Instantly share code, notes, and snippets.

@muhammedbasilsk
Created September 24, 2017 16:17
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 muhammedbasilsk/ce4e559d0b95af1a364170ab75cd9d8a to your computer and use it in GitHub Desktop.
Save muhammedbasilsk/ce4e559d0b95af1a364170ab75cd9d8a to your computer and use it in GitHub Desktop.
Linked list implementation in python
class Node(object):
def __init__(self, data):
self.data = data;
self.nextNode = None;
class LinkedList(object):
def __init__(self):
self.head = None;
self.size = 0;
def insertStart(self, data):
self.size = self.size + 1;
newNode = Node(data);
if not self.head:
self.head = newNode;
else:
newNode.nextNode = self.head;
self.head = newNode;
def findSize(self):
return self.size;
def getSize(self):
actualNode = self.head;
size = 0;
while actualNode is not None:
size += 1;
actualNode = actualNode.nextNode;
return size;
def insertEnd(self, data):
self.size += 1;
newNode = Node(data);
actualNode = self.head;
while actualNode.nextNode is not None:
actualNode = actualNode.nextNode;
actualNode.nextNode = newNode;
def traverseList(self):
actualNode = self.head;
while actualNode is not None:
print(actualNode.data);
actualNode = actualNode.nextNode;
def remove(self, data):
if self.head is None:
return;
self.size -= 1;
currentNode = self.head;
previousNode = None;
while currentNode.data != data:
previousNode = currentNode;
currentNode = currentNode.nextNode;
if previousNode is None:
self.head = currentNode.nextNode;
else:
previousNode.nextNode = currentNode.nextNode;
linkedListData = LinkedList();
linkedListData.insertStart(12);
linkedListData.insertEnd(13);
linkedListData.insertEnd(87);
linkedListData.remove(12);
linkedListData.remove(87);
linkedListData.traverseList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment