Linked List Part-3

lancelot03

Lancelot03

Posted on August 10, 2022

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
💖 💪 🙅 🚩
lancelot03
Lancelot03

Posted on August 10, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related