Linked List in Python
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 = Noneclass 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 = Nonedef insertFirst(self, data):
self.head = Node(data)
self.last = self.headdef size(self):
count = 0
node = self.head
while node:
count += 1
node = node.next
return countdef getFirst(self):
return self.headdef getLast(self):
return self.lastdef clear(self):
self.head = Nonedef insertLast(self, data):
if self.last:
self.last.next = Node(data)
self.last = self.last.next
else:
self.head = Node(data)
self.last = self.headdef getAtIndex(self, index):
node = self.head
count = 0
while count != (index):
node = node.next
count += 1
return nodedef 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.headelif 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 = atIndexdef printList(self):
node = self.head
while node:
print(node.data)
node = node.nextdef removeFirst(self):
if not self.head:
raise IndexError
else:
removedValue = self.head.data
self.head = self.head.next
return removedValuedef 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 valDeleteddef 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)