struct node
{
        int data;
        struct node *link;
};

node* CreateNode(int val)
{
        node* root = (node*)malloc(sizeof(struct node));
        root->data = val;
        root->link = NULL;
        return root;
}

node* createList(int *arr, int n)
{
        node * root = CreateNode(arr[0]);
        node * temp = root;
        for (int i =1; i < n; ++i)
        {
                temp->link = CreateNode(arr[i]);
                temp = temp->link;
        }
        return root;
}

void deleteList(node *root)
{
        if(!root) return;
        deleteList(root->link);
        free(root);
}

void printList(node *root)
{
        while(root)
        {
                printf("%d -> ", root->data);
                root= root->link;
        }
        printf("NULL\n");
}

void reverseK(node *root, node **head, node **tail, int i, int K)
{
        if(!root->link)
                *head = root;
        else
        {
                reverseK(root->link, head, tail, (i+1)%K, K);
        
                if(i == K-1)
                {
                        *tail = *head;
                        *head = root;
                }
                else
                {
                        root->link->link= root;
                        if(i == 0) root->link = *tail;
                }
        }
}

node* reverseKSize(node *root, int K)
{
        if(!root) return NULL;
        node *head = NULL;
        node *tail = NULL;
        reverseK(root, &head, &tail, 0, K);
        return head;
}

int _tmain(int argc, _TCHAR* argv[])
{
        int a[11] = {1,2,3,4,5,6,7,8,9,10,11};
        node* root = createList(a, 11);
        printList(root);
        root = reverseKSize(root, 2);
        printList(root);
        deleteList(root);

        return 0;
}

Best Regards
Ashish Goel
"Think positive and find fuel in failure"
+919985813081
+919966006652


On Tue, Jan 24, 2012 at 2:30 AM, Lucifer <sourabhd2...@gmail.com> wrote:

> @above
>
> attaching the file..
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/algogeeks/-/YW_phbT3me4J.
>
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to