Deleting first node of a linear linked list

I came across the following code for deleting the first node of a linked list:

Node* removeFirstNode(struct Node* head)
{
    if (head == NULL)
        return NULL;
  
    // Move the head pointer to the next node
    Node* temp = head;
    head = head->next;
  
    delete temp;
  
    return head;
}

Can someone explain me:

  1. The significance of using the temp variable i.e. could we use this code without temp variable

2.What does the assignment head=head->next do?

  1. What is the relation between head and temp i.e. is temp a pointer to head?

  2. Is head also a pointer to the first node of a linked list?

  3. If answer to third and fourth part both is yes, then doesn’t temp become a double pointer (pointer to a pointer)?

Answer

  1. The temp pointer is needed because the memory for the first node in the linked list needs to be freed using delete after head is reassigned. This prevents a memory leak when calling the function.

  2. The assignment head = head->next moves the beginning of the linked list to the second node, before the first node is deleted.

  3. The assignment Node *temp = head makes temp point to the same address as head, but after head is reassigned (see point 2) the pointers don’t point to the same address anymore.

  4. Yes, head is the pointer to the beginning of the linked list.

  5. No temp is never of type Node **, but head is reassigned (see point 3).