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)

Become a ModeratorHey! We would love your help!

Fill out this survey and help us moderate our community by becoming a tag moderator here at DEV.