Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programming101

Learn everything about programming

Header Linked list in Data structure

YASH PAL, 23 May 202028 May 2024

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 in data structure

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.

header linked list in DSA

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
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes