# Deleting a Node

```c
void delete(struct node *ptr, int position) {

    // If the current head node is to be deleted
    if (position == 1) {
        while (ptr -> next != head) {
            ptr = ptr -> next;
        }
        if (ptr == head) {
            free(head);
            head = NULL;
        } else {
            // Change the link
            ptr -> next = head -> next;
            // Deallocate the memory
            free(head);
            // Change head
            head = ptr -> next;
        }
    }
    
    // If node from any other position is to be deleted
    else {
        // Traverse to the required node
        for (int i = 0; i < position - 2; i++) {
            ptr = ptr -> next;
        }
        // Pointer for the node to be deleted
        struct node *toDelete;
        toDelete = ptr -> next;
        // Change the link
        ptr -> next = toDelete -> next;
        // Deallocate the memory
        free(toDelete);
    }

}
```

Contributed by Nitin Ranganath


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nitinranganath.gitbook.io/data-structures/cirular-linked-list/deleting-a-node.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
