> For the complete documentation index, see [llms.txt](https://nitinranganath.gitbook.io/data-structures/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nitinranganath.gitbook.io/data-structures/linked-list/finding-the-middle-node.md).

# Finding the Middle  Node

### First Method : Finding Length & Then Middle Element

* In the first scan of the linked list, find the length of linked list.
* Find the ceiling value of length/2.
* Traverse to the node having that index.

```c
int findMid(struct node *ptr) {

    int len = 0;
    // Finding the length of linked list
    while (ptr != NULL) {
        len++;
        ptr = ptr -> next;
    }
    // Find the middle position
    int mid = ceil(len/2);
    // Resetting ptr to head
    ptr  = head;
    // Return the middle element
    for (int i = 0; i < mid; i++) {
        ptr = ptr -> next;
    }
    return ptr -> data;

}
```

### Second Method : Using Two Pointers

* Take two pointers with initial value as the head address.
* Move pointer 2 two times in each iteration while it is not NULL.
* If pointer 2 is not NULL, move pointer 1 once.

```c
int findMid() {

    // Two pointers
    struct node *ptr1 = head;
    struct node *ptr2 = head;
    
    // Move until pointer 2 becomes NULL
    while (ptr2 != NULL) {
        // Move pointer 2 two times
        ptr2 = ptr2 -> next;
        // Don't move again if already NULL
        if (ptr2 != NULL) {
            ptr2 = ptr2 -> next;
        }
        // Move pointer 1 if pointer 2 is not NULL
        if (ptr2 != NULL) {
            ptr1 = ptr1 -> next;
        }
    }
    return ptr1 -> data;

}
```

Contributed by Nitin Ranganath


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://nitinranganath.gitbook.io/data-structures/linked-list/finding-the-middle-node.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
