Linked List in Python

Shubham Singh
3 min readMar 22, 2020

--

Happy Coding !!

In this blog, I’ll help you create Linked List using OOPs in python.

#!/usr/bin/env python3class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None

The above code you see is all you need to create a Linked List Data Structure. Everything else that we write is the operations on Linked List.

Some of the operations on Linked List that I’ll implement in the next code template includes,
1. Insert the First Element i.e. the head or root element.
2. Append nodes to list from the end.
3. Append at the start of the list.
4. Insert node at any position in the list.
5. Remove the node at any given index.
6. Remove the last node in the list.
7. Remove the first node in the list.
8. Get node at any given index in the list.
9. Size of List.
10. Remove all elements from the list.

#!/usr/bin/env python3
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.last = None
def insertFirst(self, data):
self.head = Node(data)
self.last = self.head
def size(self):
count = 0
node = self.head
while node:
count += 1
node = node.next
return count
def getFirst(self):
return self.head
def getLast(self):
return self.last
def clear(self):
self.head = None
def insertLast(self, data):
if self.last:
self.last.next = Node(data)
self.last = self.last.next
else:
self.head = Node(data)
self.last = self.head
def getAtIndex(self, index):
node = self.head
count = 0
while count != (index):
node = node.next
count += 1
return node
def insertAt(self, data, index):
if index == 0:
node = Node(data)
if self.head:
temp = self.head
node.next = temp
self.head = node
else:
self.head = node
self.last = self.head
elif index >= self.size():
self.insertLast(data)
else:
new_node = Node(data)
beforeIndex = self.getAtIndex(index - 1)
atIndex = beforeIndex.next
beforeIndex.next = new_node
new_node.next = atIndex
def printList(self):
node = self.head
while node:
print(node.data)
node = node.next
def removeFirst(self):
if not self.head:
raise IndexError
else:
removedValue = self.head.data
self.head = self.head.next
return removedValue
def removeLast(self):
if not self.head:
raise IndexError
return
if not self.head.next:
self.head = None
return
prev_node = self.head
curr_node = self.head.next
while curr_node.next:
prev_node = curr_node
curr_node = curr_node.next
valDeleted = curr_node.data
self.last = prev_node
prev_node.next = None
return valDeleted
def removeAtIndex(self, index):
if index == 0:
self.removeFirst()
elif index == (self.size() - 1):
self.removeLast()
else:
prev_node = self.getAtIndex(index - 1)
next_node = prev_node.next.next
prev_node.next = next_node
# -------------------- TEST THE LINKED LIST DS --------------------
list = LinkedList()
list.insertFirst(5)
list.insertLast(7)
list.insertLast(9)
list.insertLast(11)
list.insertLast(13)
list.insertAt(25, 0)
list.insertAt(35, 6)
list.insertAt(45, 3)
print("Size of List is {}".format(list.size()))
print("First element of list is {}".format(list.getFirst().data))
print("Last element of list is {}".format(list.getLast().data))
print("--------------------LIST ELEMETS--------------------")
list.printList()
print("Get element at index 3", list.getAtIndex(3).data)
print("Removing first element from list", list.removeFirst())
print("Remove last element from list", list.removeLast())
print("Last Element is", list.getLast().data)
print("Remove last element from list", list.removeLast())print("Last Element is", list.getLast().data)

--

--

No responses yet