Deploy your apps to a supercloud in a few clicks

This Engineering Education program is supported by Section. Instantly deploy your GitHub apps, Docker containers or K8s namespaces to a supercloud.

Try It For Free

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. 😏

Thanos Meme, 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:

Table of Contents

Linked List: Introduction

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.

Node, Linked List
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.

Null, Linked List

Uses of Linked Lists

  • 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.

Implementing Linked Lists

There are two main types of Linked Lists:

  • Singly Linked Lists
  • Doubly Linked Lists

Singly Linked Lists

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

  •, 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): = data = None

# Class to create a Linked List
class LinkedList(object):
	def __init__(self, head=None):
		self.head = head

	# Search an element and print its index
	def search(self, head, data, index):
		if == data:
			print (index)
			# Make recursive calls
				return, data, index+1)
				raise ValueError("Node not in linked list")

	# Print the linked list
	def print_list(self):
		if self.head == None:
			raise ValueError("List is empty")

		current = self.head 
			print (, end="  ")
			current =
		print ('\n')

	# Find length of Linked List
	def size(self):
		if self.head == None:
			return 0

		size = 0
		current = self.head
			size += 1
			current =

		return size

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

	# Delete a node in a linked list
	def delete(self, data):
		if not self.head:
		temp = self.head
		# Check if head node is to be deleted
		if == data:
			head =
			print ("Deleted node is " + str(

			if ( == data):
				print ("Node deleted is " + str( =
			temp =
		print ("Node not found")

Doubly Linked Lists

A doubly linked list is similar to a singly linked list. It differs in that it also contains a link to the previous node.

Doubly Linked List
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.
  • -> Prints the linked list.
class Node:
    def __init__(self, val):
        self.value = val = None
        self.prev = None
class DoublyList:
    def __init__(self, val):
        self.head = Node(val)
        self.tail = self.head

    def addNodeLast(self, val):
        current = self.head
        while != None:
            current =
        newNode = Node(val) = newNode
        newNode.prev = current
        self.tail = newNode

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

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

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

    def show(self):
        current = self.head
        while current != None:
            current =

Practice Linked Lists

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.


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.