DEV Community

Lancelot03
Lancelot03

Posted on

Linked List Part-3

find the middle element of a singly linked list without iterating the list more than once?

Solution- ### Python

Node* getMiddle(Node *head)
{
     struct Node *slow = head;
     struct Node *fast = head;

     if (head)
     {
         while (fast != NULL && fast->next != NULL)
         {
             fast = fast->next->next;
             slow = slow->next;
         }
     }
     return slow;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)