# Using the Linked List Data Structure in Python

##### July 29, 2020

We’re almost at the end of the line with this series, having mastered all the Linear Data Structures in Python. To finish things off, we have the most sophisticated Linear Data Structure, the mighty Linked List. 😏 As the meme says, the head is the most integral part of the linked list!

Well, the Linked List is not as sophisticated as you think. It’s extremely powerful though.

For more background on the different types of data structures in Python, check out the following articles:

A Linked List is a linear data structure. They are a sequence of data, connected together by links or pointers.

Linked Lists are a chain of nodes, connected together by links. Every node (fundamental block of a linked list) contains the following fields:

• Data -> The item to be stored in the node.
• Next -> The link or reference to the next node. In a linked list, the first node is called the head and the last node is determined by the condition that the next points to a null value. • Due to their dynamic size allocation and ease of insertion/deletion, linked lists are applied in a lot of use cases.
• They’re used to implement a lot of complex data structures like the adjacency list in graphs.
• They are used for lifecycle management in operating systems.
• A playlist in a music application is implemented using a doubly linked list.
• Blockchain, a complex data structure that is used for cryptocurrencies and ledgers use a linked list at their core.

There are two main types of Linked Lists:

In the following example, we’ll implement a singly linked list from scratch in Python. This contains the following methods:

• `ll.search(head, data)` -> Search the given element in the Linked List.
• `ll.print_list()` -> Print the linked list.
• `ll.size()` -> Return the length of the linked list.
• `ll.insert(ele)` -> Insert the given node into the linked list.
• `ll.delete(data)` -> Delete the given element from the linked list.
``````class Node(object):
def __init__(self, data):
self.data = data
self.next = None

# Class to create a Linked List

# Search an element and print its index
print (index)
else:
# Make recursive calls
else:
raise ValueError("Node not in linked list")

def print_list(self):
raise ValueError("List is empty")

while(current):
print (current.data, end="  ")
current = current.next
print ('\n')

# Find length of Linked List
def size(self):
return 0

size = 0
while(current):
size += 1
current = current.next

return size

# Insert a node in a linked list
def insert(self, data):
node = Node(data)
else:

# Delete a node in a linked list
def delete(self, data):
return

# Check if head node is to be deleted
print ("Deleted node is " + str(head.data))
return

while(temp.next):
if (temp.next.data == data):
print ("Node deleted is " + str(temp.next.data))
temp.next = temp.next.next
return
temp = temp.next
return
``````

A doubly linked list is similar to a singly linked list. It differs in that it also contains a link to the previous node. We implement the following methods for the Doubly Linked List data structure:

• `dll.addNodeLast(x)` -> Adds a node at the right end of the linked list.
• `dll.insertNode(pos, x)` -> Adds a node at the position specified.
• `dll.removeNode(x)` -> Removes the specified node.
• `dll.showReverse()` -> Prints the linked list in reverse.
• `dll.show()` -> Prints the linked list.
``````class Node:
def __init__(self, val):
self.value = val
self.next = None
self.prev = None

class DoublyList:
def __init__(self, val):

while current.next != None:
current = current.next
newNode = Node(val)
current.next = newNode
newNode.prev = current
self.tail = newNode

def insertNode(self, val, newVal):
if self.tail.value == val:
newNode = Node(newVal)
newNode.next.prev = newNode
else:
while current.value != val:
current = current.next
newNode = Node(newVal)
newNode.next = current.next
newNode.next.prev = newNode
newNode.prev = current
current.next = newNode

def removeNode(self, val):
elif self.tail.value == val:
self.tail = self.tail.prev
self.tail.next = None
else:
while current.value != val:
current = current.next
current.prev.next = current.next
current.next.prev = current.prev

def showReverse(self):
current = self.tail
while current != None:
print(current.value)
current = current.prev

def show(self):
while current != None:
print(current.value)
current = current.next
``````

First, try implementing the Linked Lists as shown above, and then try running them. Once you’ve mastered the implementation, try the given problem-sets to master linked lists.

## Conclusion

Linked Lists can be a little intimidating, but once you understand them you’ll find it easy to understand trees, graphs, and other such data structures! Congratulations, you have mastered linear data structures by the end of this article series. Saiharsha Balasubramaniam