Search code examples
algorithmlinked-listspace-complexity

Reversing a singly linked list when a block size is given


There is a singly connected linked list and a block size is given.For eg if my linked list is 1->2->3->4->5->6->7->8-NULL and my block size is 4 then reverse the first 4 elements and then the second 4 elements.The output of the problem should be 4->3->2->1->8->7->6->5-NULL

I was thinking of dividing the linked list into segments of size 4 and then reversing it. But that way I am forced to use a lot of extra nodes which is not desired at all. The space complexity should be kept to a minimum.

It will be highly appreciable if someone can come with a better solution where the usage of extra nodes would be kept to a minimum.


Solution

  • I tried this...seems to work fine...

    node* reverse(node* head) // function to reverse a list
    {
        node* new_head = NULL;
        while(head != NULL)
        {
            node* next = head->next;
            head->next = new_head;
            new_head = head;
            head = next;
        }
        return new_head;
    }
    
    node* reverse_by_block(node* head, int block)
    {
        if(head == NULL)
                return head;
    
        node* tmp = head;
        node* new_head = head;
        node* new_tail = NULL;
    
        int count = block;
        while(tmp != NULL && count--)
        {
            new_tail = tmp;
            tmp = tmp->next;
        }
    
        new_tail->next = NULL;
        new_tail = new_head;
        new_head = reverse(new_head);
        new_tail->next = reverse_by_block(tmp,block);
    
        return new_head;
    }