How to Implement Singly Linked List in Python

WHAT TO KNOW - Sep 9 - - Dev Community

<!DOCTYPE html>





Implementing Singly Linked Lists in Python

<br> body {<br> font-family: sans-serif;<br> line-height: 1.6;<br> }<br> h1, h2, h3 {<br> margin-bottom: 1em;<br> }<br> code {<br> background-color: #f2f2f2;<br> padding: 2px 5px;<br> border-radius: 3px;<br> }<br> pre {<br> background-color: #f2f2f2;<br> padding: 10px;<br> border-radius: 5px;<br> overflow-x: auto;<br> }<br> img {<br> max-width: 100%;<br> display: block;<br> margin: 0 auto;<br> }<br>



Implementing Singly Linked Lists in Python



Singly linked lists are a fundamental data structure in computer science, offering a dynamic and versatile way to store and manage collections of data. In Python, their implementation is straightforward and provides a valuable tool for various programming tasks. This article will guide you through the process of creating, manipulating, and utilizing singly linked lists in Python.



Introduction to Singly Linked Lists



A singly linked list is a linear data structure consisting of a sequence of nodes, each containing two primary components:


  • Data: The actual information stored in the node.
  • Next Pointer: A reference to the next node in the sequence. This pointer can be null or None, indicating the end of the list.


The first node in the list is referred to as the head, and the last node is called the tail. The head node is used to access the entire list, while the tail node's "next" pointer points to None.


Singly Linked List Diagram


Advantages of Singly Linked Lists

  • Dynamic Memory Allocation: Linked lists can grow or shrink dynamically as needed, unlike arrays with fixed sizes.
    • Efficient Insertion and Deletion: Adding or removing nodes at the beginning or middle of a linked list is relatively simple and efficient.
    • Flexibility: They allow for the storage of heterogeneous data types within the same list.

      Disadvantages of Singly Linked Lists

  • Random Access Difficulty: Accessing a specific node in the middle of the list requires traversing from the head node, making random access less efficient compared to arrays.
    • Additional Memory Usage: Each node requires additional memory for the "next" pointer.
    • Potential for Memory Leaks: If nodes are not properly deallocated after use, it can lead to memory leaks.

      Implementation in Python

      Let's now delve into the practical implementation of a singly linked list in Python.

    • Node Class

      We begin by defining a Node class to represent individual nodes in the linked list:

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


This class initializes each node with its data and sets the "next" pointer to None initially.


  1. Linked List Class

Next, we define a Linked List class to manage the entire list structure:

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

    # Insert at the beginning of the list
    def insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

    # Insert at the end of the list
    def insert_at_end(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        temp = self.head
        while temp.next is not None:
            temp = temp.next
        temp.next = new_node

    # Traverse and print the list
    def print_list(self):
        temp = self.head
        while temp:
            print(temp.data, end=" ")
            temp = temp.next
        print()

    # Search for a specific value in the list
    def search(self, data):
        temp = self.head
        while temp:
            if temp.data == data:
                return True
            temp = temp.next
        return False

    # Delete a node with a specific value
    def delete(self, data):
        temp = self.head
        prev = None
        while temp:
            if temp.data == data:
                if prev is None:
                    self.head = temp.next
                else:
                    prev.next = temp.next
                return
            prev = temp
            temp = temp.next

  1. Usage Example

Here's an example demonstrating how to use the Linked List class:

# Create a linked list object
my_list = LinkedList()

# Insert nodes
my_list.insert_at_beginning(10)
my_list.insert_at_end(20)
my_list.insert_at_beginning(5)

# Print the list
my_list.print_list()  # Output: 5 10 20 

# Search for a value
print(my_list.search(10))  # Output: True
print(my_list.search(30))  # Output: False

# Delete a node
my_list.delete(10)
my_list.print_list()  # Output: 5 20 


Additional Operations



While the provided code covers essential operations, you can extend the LinkedList class with further functionalities, such as:

  • Insertion at a specific index: Insert a node at a given position within the list.
    • Deletion at a specific index: Remove the node at a particular index.
    • Length of the list: Calculate the total number of nodes in the list.
    • Reverse the list: Reverse the order of nodes in the list.
    • Merge two linked lists: Combine two linked lists into one.

      Example: Insertion at a specific index

    def insert_at_index(self, data, index):
        if index &lt; 0 or index &gt; self.get_length():
            return False
        new_node = Node(data)
        if index == 0:
            new_node.next = self.head
            self.head = new_node
            return True
        count = 0
        temp = self.head
        prev = None
        while temp:
            if count == index:
                new_node.next = temp
                prev.next = new_node
                return True
            prev = temp
            temp = temp.next
            count += 1
        return False


Example: Length of the list


    def get_length(self):
        count = 0
        temp = self.head
        while temp:
            count += 1
            temp = temp.next
        return count




Conclusion





Singly linked lists offer a flexible and dynamic approach to data organization in Python. By understanding the concepts of nodes, pointers, and the basic operations, you can effectively implement and manipulate linked lists to solve a range of programming problems. Remember to consider the advantages and disadvantages of linked lists compared to other data structures, and choose the most appropriate one for your specific application.




. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player