How to write the functions that modify the head pointer of a Linked List in C?

Malay Gain
Last Updated: May 13, 2022

Introduction

There are many such operations where we need to modify the head pointer of a linked list to solve linked list-related problems. But there are also some cases where we can come up with a solution without modifying the head pointer of the linked list. So, we got two types of functions. One is where we don’t need to modify the head pointer of the linked list, and another one is where we will modify the linked list’s head pointer.

Here, we will be only focusing on the functions that modify the head pointer of the linked list.

There are mainly three approaches to how we use a function to modify the head pointer. Before discussing these approaches, let’s see the cases where we need to modify the head pointer.

 

Cases where modification of head pointer required

If we are inserting or deleting a node from the front of a linked list, in that case, we need to modify the head pointer to point to the new head of the linked list.

 

Approaches

We will now see the three different approaches on insertion operation where we need to modify the head pointer. 

 

Definition of node structure

 

typedef structure node{
int data;

structure node* next;
node;

 

Method 1
 

Declaring head as a global variable

We can declare the head pointer of the linked list as global so that it can be accessed from the function where we will modify the head pointer.

 

void insert_front(int new_val)
{
node* new_node=(node*)malloc(sizeof(node));
new_node->data=new_val;
new_node->next=head;

head=new_node;
}

 

But there will be issues to maintaining head pointers if we are dealing with multiple linked lists.

 

Method 2

In this approach, we take the head node pointer as a parameter and return the updated head pointer from the function.

But there is one issue if the programmer forgets to assign the return value to the head pointer where it is called, then we will lose the actual head.

 

node*  insert_front(node* head,int new_val)
{
node* new_node=(node*)malloc(sizeof(node));

new_node->data=new_val;
new_node->next=head;

head=new_node;

return node;
}

 

Method 3

In this approach, we will modify the head pointer inside the function. So, we need to use a double-pointer in the function as an argument. We will pass the address of the head pointer  when the function will be 

 

void insert_front(node** head,int new_val)
{
node* new_node=(node*)malloc(sizeof(node));
new_node->data=new_val;
new_node->next=*head;

*head=new_node;
}

 

FAQs

 

1). Discuss the function where we don’t need to modify the head pointer.

The function is used for printing a linked list or updating data members of a node in the linked list.

 

2). What is a double pointer?

It is pointer of a pointer, i.e., it stores the address of a pointer.

 

3). What is linked list?

A linked list is a dynamic data structure where each element (called a node) is made up of two items: the data and a reference (or pointer), which points to the next node. A linked list is a collection of nodes where each node is connected to the next node through its pointer.

 

4). How many types of linked lists are there?

There are mainly four types of linked lists.

  • Singly-linked list
  • Doubly linked list
  • Circular linked list
  • Doubly Circular linked list

 

Key Takeaways

This article covered how to modify the head pointer of a Linked List. Having a good grasp of Linked Lists is a huge plus point in a coding interview.

 

Check out the CodeStudio library for getting a better hold of the data structures and algorithms.

 

Side by side, you can also practice a wide variety of coding questions commonly asked in interviews in CodeStudio. Along with coding questions, you can also find the interview experience of scholars working in renowned product-based companies here. 

 

Happy learning!

 

 

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think