Python Program to Create a Linked List & Display the Elements in the List

A linked list is a linear data structure where elements are stored in nodes, and each node contains data and a reference to the next node. Unlike arrays, linked list elements are not stored in contiguous memory locations.

Below is a demonstration of creating a linked list and displaying its elements ?

Node Class Structure

First, we create a Node class to represent individual elements ?

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

# Create a single node
node = Node(10)
print(f"Node data: {node.data}")
print(f"Next pointer: {node.next}")
Node data: 10
Next pointer: None

Linked List Implementation

Now we create the linked list class with methods to add and display elements ?

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

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

    def add_value(self, data):
        if self.last_node is None:
            self.head = Node(data)
            self.last_node = self.head
        else:
            self.last_node.next = Node(data)
            self.last_node = self.last_node.next

    def display(self):
        current = self.head
        elements = []
        while current is not None:
            elements.append(str(current.data))
            current = current.next
        return " -> ".join(elements) + " -> None"

# Example usage
linked_list = LinkedList()
linked_list.add_value(6)
linked_list.add_value(7)
linked_list.add_value(8)
linked_list.add_value(9)

print("The linked list is:")
print(linked_list.display())
The linked list is:
6 -> 7 -> 8 -> 9 -> None

Visual Representation

6 7 8 9 NULL HEAD

Complete Example with User Input

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

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

    def add_value(self, data):
        if self.last_node is None:
            self.head = Node(data)
            self.last_node = self.head
        else:
            self.last_node.next = Node(data)
            self.last_node = self.last_node.next

    def print_list(self):
        current = self.head
        while current is not None:
            print(current.data)
            current = current.next

# Create linked list instance
my_list = LinkedList()

# Get user input
n = int(input('How many elements should be added? '))
for i in range(n):
    data = int(input('Enter a data value: '))
    my_list.add_value(data)

print('The linked list is:')
my_list.print_list()

Key Operations Explained

Operation Time Complexity Description
Add at end O(1) Using last_node reference
Display all O(n) Traverse from head to end
Search O(n) Linear traversal required

Conclusion

A linked list provides dynamic memory allocation and efficient insertion/deletion operations. The implementation requires a Node class to store data and references, plus a LinkedList class to manage the structure.

Updated on: 2026-03-25T18:35:29+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements