Saturday 11 January 2014

Get the middle node in a given singly linked list?


struct node
{
   int info;
   struct node *next;
};

struct node* getMiddleNode(struct node **start)
{
  struct node *middle = *start;
  struct node *fast_ptr = *start;

  if(*start != NULL)
  {
       while((fast_ptr->next) !=NULL && (fast_ptr->next->next) != NULL)
       {
          fast_ptr = fast_ptr->next->next;
          middle middle->next;
       }
  }

   return middle ;
}

No comments: