🗂️
Data Structures
  • Data Structures Manual
  • Arrays
    • Array ADT
    • Linear Search
    • Binary Search
    • Some More Basic Operations
    • Reversing an Array
    • Operations in a Sorted Array
    • Merging Two Arrays
    • Set Operations
    • Finding Missing Elements
    • Duplicates in an Array
    • Getting a Pair whose Sum = K
    • Finding Max & Min in Single Scan
  • Strings
    • Finding the Length of a String
    • Changing Cases in a String
    • Finding Number of Vowels, Consonants & Words
    • Reversing a String
    • Checking for Palindrome
    • Duplicates in a String
    • Checking if Strings are Anagrams
    • Permutations of a String
  • Singly Linked List
    • Displaying the Nodes
    • Counting the Nodes
    • Sum of all Nodes
    • Finding the Maximum Element
    • Searching in a Node
    • Inserting a Node
    • Inserting a Node in Sorted List
    • Deleting a Node
    • Checking if List is Sorted
    • Removing Duplicates from a List
    • Reversing a Linked List
    • Concatenating Two Lists
    • Detecting a Loop in Linked List
    • Merge Two Sorted Lists
    • Finding the Middle Node
  • Cirular Linked List
    • Displaying the Nodes
    • Inserting a Node
    • Deleting a Node
  • Doubly Linked List
    • Inserting a Node
    • Deleting a Node
    • Reversing a Doubly Linked List
    • Circular Doubly Linked List
  • Stack
    • Stack Using Array
    • Stack Using Linked List
    • Balancing Parenthesis
    • Infix to Postfix
    • Evaluation of Postfix Expression
  • Queue
    • Queue using Array
    • Queue using Linked List
    • Double Ended Queue
  • Binary Tree
    • Creating a Binary Tree using Queue
    • Recursive Tree Traversals
    • Iterative Tree Traversals
    • Level Order Traversal
    • Counting Nodes in a Binary Tree
    • Finding the Height of Tree
    • Finding Sum of All Nodes
  • Binary Search Tree
    • Searching in a BST
    • Inserting in a BST
    • Deleting in a BST
  • AVL Tree
    • Inserting in an AVL Tree
    • AVL Tree Rotations
    • Deleting in an AVL Tree
  • Heap
    • Inserting in a Heap
    • Deleting in a Heap
    • Heapify
Powered by GitBook
On this page
  • First Method : Finding Length & Then Middle Element
  • Second Method : Using Two Pointers

Was this helpful?

  1. Singly Linked List

Finding the Middle Node

Procedure to find the middle node of a singly linked list.

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.

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.

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

PreviousMerge Two Sorted ListsNextDisplaying the Nodes

Last updated 4 years ago

Was this helpful?