How to write C functions that modify head pointer of a Linked List?
Last Updated :
10 Jan, 2023
Consider simple representation (without any dummy node) of Linked List. Functions that operate on such Linked lists can be divided into two categories:
1) Functions that do not modify the head pointer: Examples of such functions include, printing a linked list, updating data members of nodes like adding given a value to all nodes, or some other operation that access/update data of nodes
It is generally easy to decide the prototype of functions of this category. We can always pass the head pointer as an argument and traverse/update the list. For example, the following function that adds x to data members of all nodes.
C
void addXtoList( struct Node *node, int x)
{
while (node != NULL)
{
node->data = node->data + x;
node = node->next;
}
}
|
2) Functions that modify the head pointer:
Examples include, inserting a node at the beginning (head pointer is always modified in this function), inserting a node at the end (head pointer is modified only when the first node is being inserted), deleting a given node (head pointer is modified when the deleted node is the first node). There may be different ways to update the head pointer in these functions. Let us discuss these ways using the following simple problem:
“Given a linked list, write a function deleteFirst() that deletes the first node of a given linked list. For example, if the list is 1->2->3->4, then it should be modified to 2->3->4”
The algorithm to solve the problem is a simple 3 step process: (a) Store the head pointer (b) change the head pointer to point to the next node (c) delete the previous head node.
Following are different ways to update the head pointer in deleteFirst() so that the list is updated everywhere.
2.1) Make head pointer global: We can make the head pointer global so that it can be accessed and updated in our function. Following is C code that uses a global head pointer.
C
struct Node *head = NULL;
void deleteFirst()
{
if (head != NULL)
{
struct Node *temp = head;
head = head->next;
free (temp);
}
}
|
See this for a complete running program that uses the above function.
This is not a recommended way as it has many problems like the following:
a) head is globally accessible, so it can be modified anywhere in your project and may lead to unpredictable results.
b) If there are multiple linked lists, then multiple global head pointers with different names are needed.
See this to know all reasons why should we avoid global variables in our projects.
2.2) Return head pointer: We can write deletefirst() in such a way that it returns the modified head pointer. Whoever is using this function, has to use the returned value to update the head node.
C
struct Node *deleteFirst( struct Node *head)
{
if (head != NULL)
{
struct Node *temp = head;
head = head->next;
free (temp);
}
return head;
}
|
See this for complete program and output.
This approach is much better than the previous 1. There is only one issue with this, if the user misses assigning the returned value to the head, then things become messy. C/C++ compilers allow calling a function without assigning the returned value.
C
head = deleteFirst(head);
deleteFirst(head);
|
2.3) Use Double Pointer: This approach follows the simple C rule: if you want to modify the local variable of one function inside another function, pass a pointer to that variable. So we can pass the pointer to the head pointer to modify the head pointer in our deletefirst() function.
C
void deleteFirst( struct Node **head_ref)
{
if (*head_ref != NULL)
{
struct Node *temp = *head_ref;
*head_ref = (*head_ref)->next;
free (temp);
}
}
|
See this for complete program and output.
This approach seems to be the best among all three as there are fewer chances of having problems.
Similar Reads
How to create a pointer to another pointer in a linked list?
In this article we cover how to create a pointer to another pointer in a linked list, it can be singly, doubly, or circularly. What is Linked List: A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked
7 min read
Move the Kth element to the Front of a Doubly Linked List
Given a doubly linked list and an integer K, you need to move the Kth element to the front of the list while maintaining the order of the other elements. Examples: Input: DLL: 2 <-> 6 <-> 3 <-> 8 <-> 11 <-> 23 <-> 7 -> NULL, K = 4Output: 8 <-> 2 <->
11 min read
Can we reverse a linked list in less than O(n)?
It is not possible to reverse a simple singly linked list in less than O(n). A simple singly linked list can only be reversed in O(n) time using recursive and iterative methods. A doubly linked list with head and tail pointers while only requiring swapping the head and tail pointers which require le
1 min read
Point arbit pointer to greatest value right side node in a linked list
Given singly linked list with every node having an additional âarbitraryâ pointer that currently points to NULL. We need to make the âarbitraryâ pointer to the greatest value node in a linked list on its right side. A Simple Solution is to traverse all nodes one by one. For every node, find the node
15+ min read
Delete a Node in a Singly Linked List with Only a Pointer
Given only a pointer to a node to be deleted in a singly linked list. The task is to delete that node from the list. Note: The given pointer does not point to the last node of the linked list. Examples: Input: LinkedList: 1->2->3->4->5, delete_ptr = 2Output: LinkedList: 1->3->4-
6 min read
Two-Pointer Technique in a Linked List
The two-pointer technique is a common technique used in linked list problems to solve a variety of problems efficiently. It involves using two pointers that traverse the linked list at different speeds or in different directions. Use Cases of Two-Pointer Technique in a Linked List:1. Finding the Mid
3 min read
Sorted insert in a doubly linked list with head and tail pointers
A Doubly linked list is a linked list that consists of a set of sequentially linked records called nodes. Each node contains two fields that are references to the previous and to the next node in the sequence of nodes. The task is to create a doubly linked list by inserting nodes such that list rema
10 min read
Cpp14 Program For Printing Nth Node From The End Of A Linked List (Duplicate)
Given a Linked List and a number n, write a function that returns the value at the n'th node from the end of the Linked List.For example, if the input is below list and n = 3, then output is "B" Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method 1 (Use length
5 min read
Program for Nth node from the end of a Linked List
Given a Linked List of M nodes and a number N, find the value at the Nth node from the end of the Linked List. If there is no Nth node from the end, print -1. Examples: Input: 1 -> 2 -> 3 -> 4, N = 3Output: 2Explanation: Node 2 is the third node from the end of the linked list. Input: 35 -
15 min read
Point to next higher value node in a linked list with an arbitrary pointer
Given a singly linked list with every node having an additional arbitrary pointer that currently points to NULL. The task is to make the arbitrary pointer point to the next higher-value node. A Simple Solution is to traverse all nodes one by one, for every node, find the node that has the next great
12 min read