Table of contents
1.
Introduction
1.1.
Cases where modification of head pointer required
2.
Approaches
2.1.
Method 1 
2.2.
Method 2
2.3.
Method 3
3.
Frequently Asked Questions
3.1.
Discuss the function where we don’t need to modify the head pointer.
3.2.
What is a double pointer?
3.3.
What is a linked list?
3.4.
How many types of linked lists are there?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

How to Write the Functions that Modify the Head Pointer of a Linked List in C?

Author Malay Gain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Linked List

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.

Also see, Data Structures and Rabin Karp Algorithm.

 

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;
You can also try this code with Online C Compiler
Run Code

 

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;
}
You can also try this code with Online C Compiler
Run Code

 

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;
}
You can also try this code with Online C Compiler
Run Code

 

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;
}
You can also try this code with Online C Compiler
Run Code

 

Frequently Asked Questions

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.

What is a double pointer?

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

What is a 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.

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

 

Conclusion

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.

Recommended Problems -

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy learning!

Live masterclass