int main()
{
    Node *head = newNode(50);
    head->next = newNode(20);
    head->next->next = newNode(15);
    head->next->next->next = newNode(4);
    head->next->next->next->next = newNode(10);
    printf("Created list: ");
    printList(head);

    /* Create a loop for testing */
    head->next->next->next->next->next = head->next->next;

    // Lets show the loop
    Node *tempNode = head;
    int count = 10; // Greater than number of elements in loop
    printf("Start element in list: %d\n", head->key);
    printf("Looped list: ");
    while(count != 0)
    {
        printf("%d ", tempNode->key);
        tempNode = tempNode->next;
        count--;
    }
    printf("\n");

    printf("Entering loop...\n");
    detectAndRemoveLoop(head);
    printf("Exiting loop...\n");

    printf("Linked List after removing loop \n");
    printList(head);

    return 0;
}
Node *detectAndRemoveLoop(Node *head)
{
    Node *node;

    if(head == head->next)
        return NULL;

    if(head->next == NULL)
        return head;

    node = head->next;
    head->next = head;

    head->next = detectAndRemoveLoop(node);

    return head;
}
int main(){
	node* head = NULL;
 
    push(&head, 10);
    push(&head, 4);
    push(&head, 15);
    push(&head, 20);
    push(&head, 50);
 
    /* Create a loop for testing */
    head->next->next->next->next->next = head->next->next;
 
    detectAndRemoveLoop(head);
 
    printf("Linked List after removing loop \n");
    printList(head);
	return 0;
}
/* Drier program to test above function*/
int main()
{
    /* Start with the empty list */
    struct node* head = NULL;

    push(&head, 10);
    push(&head, 4);
    push(&head, 15);
    push(&head, 20);
    push(&head, 50);

    /* Create a loop for testing */
    head->next->next->next->next->next = head->next->next;

    detectAndRemoveLoop(head);

    printf("Linked List after removing loop \n");
    printList(head);

    getchar();
    return 0;
}