What I Learned Today: Traversing and Reversing Singly Linked Lists in Python

kasumbi philkasumbi phil
3 min read

Hey everyone! 👋

Today, I dove into the world of linked lists, one of the fundamental data structures in computer science. Specifically, I focused on singly linked lists, learning how to traverse and reverse them using Python.


What is a Singly Linked List?

A singly linked list is a sequence of nodes where each node contains two parts:

  • Value: The data it stores.

  • Next: A reference (or pointer) to the next node in the list.

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

Example:

10 -> 20 -> 30 -> 40 -> None

Traversing a Linked List

Traversal means visiting every node in the list, starting from the head and moving through each node by following the .next pointer until you reach the end.

Here’s the code I wrote to traverse a linked list and print its values:

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

    def __str__(self):
        return f"nodes 1:{self.value}"


Node1  = Node(10)
Node2  = Node(20)
Node3  = Node(30)
Node4  = Node(40)

Node1.next = Node2
Node2.next = Node3
Node3.next = Node4

head = Node1

current = head

while current is not None:
    print(current.value)
    current = current.next


current = head
while current is not None:
    print(current.value)
    current = current.next

This outputs:

10
20
30
40

Reversing a Linked List

Reversing a linked list means changing the direction of the .next pointers so that the list order is reversed.

For example:

Original: 10 -> 20 -> 30 -> 40 -> None
Reversed: 40 -> 30 -> 20 -> 10 -> None

I implemented the reversal with three pointers:

  • prev to track the previous node,

  • current for the current node being processed,

  • next_node to temporarily store the next node.

The code looks like this:

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

    def __str__(self):
        return f"Node: {self.value}"

Node1  = Node(10)
Node2  = Node(20)
Node3  = Node(30)
Node4  = Node(40)

Node1.next = Node2
Node2.next = Node3
Node3.next = Node4

head = Node1

prev = None
current = head

while current is not None:
    next_node = current.next
    current.next = prev
    prev = current
    current = next_node 

    if current:
        print(current.value)

# set the new head
head = prev


print("\nReversed Linked List:")
current = head
while current:
    print(current.value)
    current = current.next

After reversing, traversing the list again prints:

40
30
20
10

Why Linked Lists Matter

Linked lists are a great way to understand pointers and dynamic memory management. Unlike arrays, linked lists don’t require contiguous memory, and insertion/deletion operations can be more efficient in some cases.

They’re used under the hood in many data structures and algorithms, including stacks, queues, and graphs.


Thanks for reading my learning journey!

0
Subscribe to my newsletter

Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

kasumbi phil
kasumbi phil