×

Python Linked List

Linked List

Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with each other through a pointer.

Linked lists are often used over arrays because of several reasons. The length of a linked list can vary concerning the need of the user. It is dynamically sized, unlike the array. We can increase or decrease its size as we need. In a linked list, we can insert elements very easily.

Every element of a linked list is known as a node, and each node contains two parts:

Data: It contains the data value in the node.

Next: It contains the pointer pointing to the next node.

There is also a concept of a head pointer, which points to the first node. In simple words, we can say that a linked list consists of a collection of nodes.

Advantages of using a linked list

  • It is dynamically sized, which mean increasing or decreasing its size as per our need.
  • Operations like insertion and deletion are very easy in a linked list.

Disadvantages of using a linked list

  • Linked list doesn’t allow random access, so we can’t access any element randomly in a linked list. We have to traverse all elements before we reach any specific element.
  • Linked list requires extra memory as compared to an array. It requires extra memory for the pointers stored along with the data value.
  • Linked lists are not cache-friendly.

Syntax of creating a node

class Node:
      # Function to initialize the node object
      def __init__ (self, data_val):
          self.data = data_val   # assign the data
          self.next = None         # set the next value as Null

Empty Linked List

An empty linked list is a linked list where the head pointer points to None.

Syntax of creating an empty linked list

class linkedList: 
   def __init__(self): 
        self.head=None

Creating a linked list

A linked list can be created by creating an object for the node and a class to use that object. Then we pass the appropriate values to the object node and start adding the nodes. Given below is an example of creating a single linked list with four data values.

In the below code, we are creating a linked list of four nodes.

class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None


class LinkedList:
   def __init__(self):
      self.headval = None


node1 = LinkedList()
node1.headval = Node("January")
e2 = Node("Feburary")
e3 = Node("March")
e4 = Node("April")
# Link first Node to second node
node1.headval.nextval = e2


# Link second Node to third node
e2.nextval = e3
# Link third Node to fourth node
e3.nextval = e4

Explanation:

In the above code, we have created an object node and then a class to use that object node. In the node class Node. We store the value and the next pointer. In the other class LinkedList(), we will create a linked list. After that, we will call the class and store the value.

Traversing a Linked List

class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None


class LinkedList:
   def __init__(self):
      self.headval = None
 
   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval


node1 = LinkedList()
node1.headval = Node("January")
e2 = Node("Feburary")
e3 = Node("March")
e4 = Node("April")
# Link first Node to second node
node1.headval.nextval = e2


# Link second Node to third node
e2.nextval = e3
# Link third Node to fourth node
e3.nextval = e4
node1.listprint()

Output:

January
February
March
April

Explanation:

In the above code, we have created the linked list and added the four values in four nodes. Then we will add another block of code to print the values. We have defined a function name listprint( ) and will print all the value using the while loop.

Insertion at the beginning

Inserting an element at the beginning of a linked list is very easy because in this you can just create a new node and add the next pointer to the first node. In this, we don’t have to traverse the entire linked list. Hence, we can insert a new node at the beginning of a linked list in just O(1) complexity.

Example:

class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None


class LinkedList:
   def __init__(self):
      self.headval = None
   # Print the linked list
   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval
   def AtBegining(self,newdata):
      NewNode = Node(newdata)


# Update the new nodes next val to existing node
      NewNode.nextval = self.headval
      self.headval = NewNode


node1 = LinkedList()
node1.headval = Node("July")
n2 = Node("August")
n3 = Node("September")


node1.headval.nextval = n2
n2.nextval = n3


node1.AtBegining("June")
node1.listprint()
 

Output:

June
July
August
Septemberer

Explanation:

In the above code, we have added another block of code for inserting the elements at the beginning of the linked list. We have used a function AtBeginning( ) to add the element at the start.

Insertion at the end

Inserting an element at the end of a single linked list can be done in a O(n) complexity. In this, we traverse the entire linked list, and then at the end, we add a new node by pointing the next value of the last node to the new node.

Example:

class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None


class LinkedList:
   def __init__(self):
      self.headval = None


# Function to add newnode
   def AtEnd(self, newdata):
      NewNode = Node(newdata)
      if self.headval is None:
         self.headval = NewNode
         return
      laste = self.headval
      while(laste.nextval):
         laste = laste.nextval
      laste.nextval=NewNode
# Print the linked list
   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval


node1 = LinkedList()
node1.headval = Node("June")
e2 = Node("July")
e3 = Node("August")


node1.headval.nextval = e2
e2.nextval = e3


node1.AtEnd("September")


node1.listprint()

Output

June 
July
August
September

Explanation:

In the above code, firstly we have created a linked list and then we have used another function AtEnd( ) to add another element at the end.

Insertion in between two nodes

class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None


class LinkedList:
   def __init__(self):
      self.headval = None


# Function to add newnode
   def Inbetween(self,middle_node,newdata):
      if middle_node is None:
         print("The mentioned node is absent")
         return


      NewNode = Node(newdata)
      NewNode.nextval = middle_node.nextval
      middle_node.nextval = NewNode
# Print the linked list
   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval


node1 = LinkedList()
node1.headval = Node("June")
e2 = Node("July")
e3 = Node("August")


node1.headval.nextval = e2
e2.nextval = e3


node1.Inbetween(node1.headval.nextval,"August")
node1.listprint()

Output

June 
July
August
September

Deleting a node

class Node:
class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None
class LinkedList:
   def __init__(self):
      self.headval = None
   def Atbegining(self, data_in):
      NewNode = Node(data_in)
      NewNode.next = self.head
      self.head = NewNode
# Function to remove node
   def RemoveNode(self, Removekey):
      HeadVal = self.head
         
      if (HeadVal is not None):
         if (HeadVal.data == Removekey):
            self.head = HeadVal.next
            HeadVal = None
            return
      while (HeadVal is not None):
         if HeadVal.data == Removekey:
            break
         prev = HeadVal
         HeadVal = HeadVal.next


      if (HeadVal == None):
         return


         prev.next = HeadVal.next
         HeadVal = None
# Print the linked list
   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval


node1 = LinkedList()
node1.headval = Node("June")
n2 = Node("July")
n3 = Node("August")


node1.headval.nextval = n2
n2.nextval = n3


node1.RemoveNode("June")
node1.Listprint()

Output:

July
August

Related Topics

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Python String ljust() method

Python String ljust() method The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default). Syntax String.ljust(width[, fillchar]) Parameter width: This parameter represents the...

2 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Python Write Excel File

Python Write Excel File The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple...

3 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

Artificial intelligence mini projects with source code in Python

Project Name: Movie recommendation system A recommendation provides customers with relevant information related to their searches. Before the recommendation system, the most common method of purchasing was to rely on the...

4 minutes read.

Python Compiler

What is Compiler? The compiler is mainly a program used to convert the source code into the machine or binary code. The source code is generally a computer program written using...

6 minutes read.

How to Open a file in python with Path

In this tutorial, we will see rather than the traditional way of opening a file by locating or navigating it from our desktops; we will learn how to open the...

2 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

Python Banner

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Event Key in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Bar Plot in Python

A bar chart or bar graph displays categorical data using rectangular bars with heights or lengths proportionate to the values for the data distributed in the dataset. In a bar...

16 minutes read.

Python String center() method

Python String center() method The center() method will center align the string, using a specified character (space is default) as the fill character. Syntax string.center(width[, fillchar]) Parameter width This parameter represents the length of the returned string. fillchar...

1 minute read.