Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Header Linked list | Data structure

YASH PAL, 23 May 202017 August 2025

Header Linked List – In Data Structure The header linked list is a list that has a header node at the beginning of the linked list. and the head always refers to the header node of the list.

What is Header Node

The info part of the header node is always None. Therefore we can use this part to store some useful information about the linked list. like storing the number of nodes in a list and some of the values of the linked list etc.

and the linked part of the header node is always referred to as the first node of the linked list.

Header linked list

Advantages of Header Linked List

If the list is empty then always the header node is available in the linked list. so we don’t need to check whether the linked list is empty or not.

Advantages of header linked list

We can easily perform operations like insertion and deletion because we don’t need to check if the list is empty or not.

Header Linked List program in Python

class Node(object):

    def __init__(self,value):
        self.info = value
        self.link = None
class HeaderLinkedList(object):

    def __init__(self):
        self.head = Node(0)

    def display_list(self):
        if self.head.link == None:
            print("List is empty")
            return
        p =self.head.link
        print("List is : ")
        while p is not None:
            print(p.info, " ",end='')
            p = p.link
        print()

    def create_list(self):
        n = int(input("Enter the number of nodes : "))
        for i in range(n):
            data = int(input("Enter the element to be inserted : "))
            self.insert_at_end(data)

    def insert_at_end(self,data):
        temp = Node(data)

        p = self.head
        while p.link is not None:
            p = p.link
        p.link = temp

    def insert_before(self,data,x):
        p = self.head
        while p.link is not None:
            if p.link.info == x:
                break            p = p.link

        if p.link is None:
            print(x, " not present in the list")
        else:
            temp = Node(data)
            temp.link = p.link
            p.link = temp

    def insert_at_position(self,data,x):
        p = self.head
        i = 1        while i <= k-1 and p is not None:
            p = p.link
            i += 1
        if p is None:
            print("You can insert only upto ", (i-1), " th position")
        else:
            temp = Node(data)
            temp.link = p.link
            p.link  = temp

    def delete_node(self,data):
        p = self.head
        while p.link is not None:
            if p.link.info == data:
                break            p = p.link

        if p.link == None:
            print(data, " not found")
        else:
            p.link = p.link.link

    def reverse_list(self):
        prev = None        p = self.head.link
        while p is not None:
            next = p.link
            p.link = prev
            prev = p
            p = next

        self.head.link = prev

list = HeaderLinkedList()

list.create_list()

while True:
    print("1. Display list")
    print("2. Insert a node at the end of the list")
    print("3. Insert a node before a specified node")
    print("4. Insert a node at a given position")
    print("5. Delete a node")
    print("6. Reverse the list")
    print("9. Quit")

    option = int(input("Enter your choice : "))

    if option == 1:
        list.display_list()
    elif option == 2:
        data = int(input("Enter the element to be inserted : "))
        list.insert_at_end(data)
    elif option == 3:
        data = int(input("Enter the element to be inserted : "))
        x = int(input("Enter the element before which to insert : "))
        list.insert_before(data,x)
    elif option == 4:
        data = int(input("Enter the element to be inserted : "))
        k = int(input("Enter the position at which to insert : "))
        list.insert_at_position(data,x)
    elif option == 5:
        data = int(input("Enter the element to be deleted : "))
        list.delete_node(data)
    elif option == 7:
        break    else:
        print("Wrong option")
    print()
Computer Science Tutorials Data Structures Tutorials computer scienceData Structure

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes