Python Linked Lists


A linked list is one of the most important data structures you will study when learning DSA. It works differently from arrays because the elements are not stored next to each other in memory. Instead, each element stores its own value and a link to the next element. This creates a flexible chain of nodes that can grow and shrink without moving other elements. Linked lists are used in operating systems, browsers, media players, queues, stacks and situations where dynamic memory usage is important.

In this tutorial, you will learn what linked lists are, how nodes work, how pointers create the connections, how to build your own linked list class in Python and how the basic operations like inserting, deleting and traversing work. You will also see real-life uses and several practice examples that show how linked lists help you manage ordered data efficiently.

What Is a Linked List?

A linked list is a sequence of nodes connected through pointers. Each node has two parts:

  • A value

  • A reference to the next node

The last node points to None, which marks the end of the list.

Unlike arrays, you do not need to declare a fixed size. Linked lists grow as you add nodes.

How Nodes Work?

A node represents one element of the linked list.

You can imagine nodes as boxes arranged in a line. Each box stores:

  1. The actual data

  2. A link to the next box

Example visual layout:

[10 | next]  →  [20 | next]  →  [30 | next]  →  None

This gives linked lists their dynamic nature.

Why Linked Lists Are Useful?

Linked lists are useful when you need efficient insertions and deletions. In arrays, adding or removing an element requires shifting many items. In a linked list, you simply change the pointers.

Use cases include:

  • Music playlist navigation

  • Undo/redo features

  • Browser history

  • Memory management in operating systems

  • Queues and stacks

These situations rely heavily on linked list behavior.

Creating a Node in Python

In Python, you begin by defining a simple Node class.

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

This class holds the value and the pointer.

Creating a Linked List Class

Next, you build your LinkedList class to manage nodes.

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

The head is the first node in the list. If the list is empty, head is None.

Inserting at the Beginning

Adding at the beginning is simple and fast.

def insert_at_beginning(self, data):
    new_node = Node(data)
    new_node.next = self.head
    self.head = new_node

This operation does not require shifting elements, which makes it efficient.

Inserting at the End

To insert at the end, you first walk through the list until you reach the last node.

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:
        temp = temp.next

    temp.next = new_node

This adds the new node as the last element.

Traversing a Linked List

Traversal is the process of walking through all nodes and reading their values.

def traverse(self):
    temp = self.head
    while temp:
        print(temp.data)
        temp = temp.next

Traversal is useful for displaying or searching.

Deleting a Node

Deleting a node requires updating the previous node’s pointer.

Delete the first node

def delete_beginning(self):
    if self.head:
        self.head = self.head.next

Delete a specific value

def delete_value(self, key):
    temp = self.head

    if temp and temp.data == key:
        self.head = temp.next
        return

    prev = None
    while temp and temp.data != key:
        prev = temp
        temp = temp.next

    if temp:
        prev.next = temp.next

This removes the node containing the given value.

Searching in a Linked List

Searching means checking if a value exists in the list.

def search(self, key):
    temp = self.head
    while temp:
        if temp.data == key:
            return True
        temp = temp.next
    return False

Linked lists do not support direct index access, so searching is linear.

Real-Life Uses of Linked Lists

Music Playlists

Songs are linked so the player can move next or previous without shifting anything.

Browser History

Pages are linked like a chain, allowing forward and backward movement.

OS Memory Allocation

Operating systems use linked lists to track free memory blocks.

Stacks and Queues

Behind the scenes, linked lists support stack and queue operations efficiently.

Image Viewer

Images are linked so you can move to the next or previous photo smoothly.

These examples highlight how often linked lists appear in daily-use programs.

Linked List Simulation: Browser History

history = LinkedList()
history.insert_at_end("Home")
history.insert_at_end("Products")
history.insert_at_end("Cart")

history.traverse()

This describes how pages are connected in order.

Linked List Simulation: Music Playlist

playlist = LinkedList()
playlist.insert_at_end("Song 1")
playlist.insert_at_end("Song 2")
playlist.insert_at_end("Song 3")

playlist.traverse()

This shows how songs play one after another.

Practical Examples

Example 1: Create a linked list

ll = LinkedList()

Example 2: Insert at beginning

ll.insert_at_beginning(10)

Example 3: Insert at end

ll.insert_at_end(20)

Example 4: Traverse list

ll.traverse()

Example 5: Delete first node

ll.delete_beginning()

Example 6: Delete a value

ll.delete_value(30)

Example 7: Search a value

ll.search(20)

Example 8: Check empty list

ll.head is None

Example 9: Combine operations

ll.insert_at_end(50)
ll.insert_at_end(60)
ll.delete_value(50)

Example 10: Full linked list workflow

ll = LinkedList()
ll.insert_at_end(5)
ll.insert_at_end(10)
ll.insert_at_end(15)
ll.traverse()

Summary of the Tutorial

A linked list is a dynamic structure made of nodes that store values and links. You learned how nodes connect through pointers, how to create and manage a linked list in Python, how to insert and delete nodes, and how traversal and searching work. Linked lists support many real applications like browser history, playlist navigation, memory handling, queues and stacks. Once you understand how linked lists function, you can use them confidently in more advanced structures like trees, graphs and hash tables.


Practice Questions

Q1. Write a Python program to create a linked list with 4 nodes having values: 10, 20, 30, and 40.

Q2. Write a Python program to define a function that displays all values in the linked list.

Q3. Write a Python program to insert a new node at the beginning of the linked list.

Q4. Write a Python program to insert a new node at the end of the linked list.

Q5. Write a Python program to delete a node with value 20 from the linked list.

Q6. Write a Python program to count the total number of nodes in the linked list.

Q7. Write a Python program to check if a specific value exists in the linked list.

Q8. Write a Python program to reverse the linked list.

Q9. Write a Python program to create a function that searches a node by value and returns its position.

Q10. Write a Python program to print the value of the last node in the linked list.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top