Linked List

Linked List

Python Linked List

ยท

3 min read

Table of contents

No heading

No headings in the article.

Linked lists are data structure that stores data called Node in form of a chain. Imagine three brothers linked together and connected by their mother. The brothers are the nodes while the mother is the next that connects the brothers. The Node in the linked list can be any value either a string, integer or float. Each node in the linked list is connected to another node with the use of the next value.

Linked list is also like an array but it differs from an array in the sense that linked lists are joined together. Linked list also differs from an array in the way they store elements in the memory. Each node in a linked list has two different fields; value - which contains element and the Next - which contains reference to the next node on the list. The first Node in a linked list is called the Head while the last node is called the Tail

There are mainly three types of linked lists:

  1. Single-linked list

  2. Double-linked list

  3. circular-linked list

To create a Single-Linked list, we start by creating the Node class

class Node:    
    def __init__(self, value):
        self.value = value
        self.next = None

In the code above I created the class Node which takes in value. The next allows us to link other nodes.

Then we create the LinkedList class right under the Node class and define basic operations

class LinkedList:
    def __init__(self):
       self.head = None 
    def addnode(self, new_val):
        """Adding element to the front of the linked list"""
        new_node = Node(new_val)
        if self.head == None:
            self.head = new_node
        else:
            new_node.next = self.head
        self.head = new_node

I created the LinkedList class in the code above. The addnode() method allows you add a new node to the linked list. Let's check how it works.

s1 = LinkedList()
s1.addnode(7)
s1.addnode(9)
s1.addnode(8)
s1.addnode(5)

From the code above 7 is the head since it is the first node we are adding to the linked list then the other elements. We don't have to call the Node class as we are already using it directly in the LinkedList class new_node = Node(self.new_val).

Let's check how to print the nodes in the linked list and how to delete the node

class LinkedList:
    def __init__(...)
    def addnode(...)
    def printnode(self):
        if self.head == None:
            print("Linked list is empty")
        else:
            val = self.head
            while val != None:
                print(str(val) + " ---> " , end ='')
                val = val.next
    def deletenode(self):
    """Deletes the first node in the linked list"""
    if self.head == None:
        print("Linked List is empty")
    else:
        self.head = self.head.next

The code explains how to print the nodes in the linked list. It checks if no node is in the linked list. Then if there are nodes in the linked list it prints them out 7 ---> 9 ---> ---> 8 ---> 5 in this form. The deletenode method allows you delete the first node in the linked list making the next node in the linked list the first Node

print("Printing the node")
s1.printnode()
s1.deletenode()
print("\n printing the node after deleting the first node ")
s1.printnode()

Check out the code on Github

Happy Learning ๐ŸŽ‰ ๐ŸŽ‰ ๐ŸŽ‰

ย