Traversal of Singly Linked List in Python

To traverse a singly linked list in Python, you simply need to iterate through each node starting from the head node and print the data of each node until you reach the end of the list (i.e. when the next pointer of a node is None).

Below is the implementation of the above idea:

Python3
# Python Program for traversal of Singly Linked list
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def insert_at_beginning(head, data):
    new_node = Node(data)
    new_node.next = head
    return new_node
        
def traverse(head):
    current = head
    while current:
        # Print the current node's data followed by an arrow and space
        print(str(current.data) + " -> ", end=" ")
        current = current.next
    # At the end of the list, print None to indicate no further nodes
    print("None")

# Singly linked list created and its head stored in a variable named "head"
head = None
head = insert_at_beginning(head, 4)
head = insert_at_beginning(head, 3)
head = insert_at_beginning(head, 2)
head = insert_at_beginning(head, 1)

# To traverse and print the nodes:
traverse(head)

Output
1 ->  2 ->  3 ->  4 ->  None

Singly Linked List in Python

Singly Linked List is a type of data structure that is made up of nodes that are created using self-referential structures. Each node contains a data element and a reference (link) to the next node in the sequence. This allows for a dynamic and efficient management of data elements.

Table of Content

  • What is a Singly Linked List?
  • Representation of Singly linked list in Python
  • Traversal of Singly Linked List in Python
  • Insertion of Singly Linked List in Python
  • Deletion of Singly Linked List in Python
  • Related Practice Problem on Singly Linked List

Similar Reads

What is a Singly Linked List?

A singly linked list is a linear data structure in which the elements are not stored in contiguous memory locations and each element is connected only to its next element using a pointer. We can traverse the entire linked list using the next pointer of each node....

Representation of Singly linked list in Python:

Here is the representation of the singly linked list in python:...

Traversal of Singly Linked List in Python:

To traverse a singly linked list in Python, you simply need to iterate through each node starting from the head node and print the data of each node until you reach the end of the list (i.e. when the next pointer of a node is None)....

Insertion of Singly Linked List in Python:

Given a Linked List, the task is to insert a new node in this given Linked List at the following positions:...

Deletion of Singly Linked List in Python:

You can delete an element in a list from:...

Related Practice Problem on Singly Linked List:

Article Link Detect and Remove Loop in a Linked List Link Detect loop or cycle in a linked list Link Program for Nth node from the end of a Linked List Link Find Middle of the Linked List Link Function to check if a singly linked list is palindrome Link Reverse a Linked List Link Remove duplicates from a sorted linked list Link Intersection point of two Linked Lists Link Add two numbers represented by Linked List Link Remove Duplicates from an Unsorted Linked List Link...