# 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: 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/linked-list/finding-the-middle-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.
