+ Reply to Thread
Results 1 to 2 of 2

Thread: Reverse every k nodes of a linked list

  1. #1
    Krazy Guest

    Reverse every k nodes of a linked list

    Write program to reverse every k nodes of a linked list.

  2. #2
    Krazy Guest
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    /* Link list node */
    struct node
    {
        int data;
        struct node* next;
    };
    
    /* Reverses the linked list in groups of size k and returns the pointer to the new head node */
    struct node *reverse (struct node *head, int k)
    {
        struct node* current = head;
        struct node* next;
        struct node* prev = NULL;
        int count = 0;   
    
        /*reverse first k nodes of the linked list */
        while (current != NULL && count < k)
        {
           next  = current->next;
           current->next = prev;
           prev = current;
           current = next;
           count++;
        }
    
        /* next is now a pointer to (k+1)th node
           Recursively call for the list starting from current.
           And make rest of the list as next of first node */
        if(next !=  NULL)
        {  head->next = reverse(next, k); }
    
        /* prev is new head of the input list */
        return prev;
    }
    
    /* UTILITY FUNCTIONS */
    /* Function to push a node */
    void push(struct node** head_ref, int new_data)
    {
        /* allocate node */
        struct node* new_node =
                (struct node*) malloc(sizeof(struct node));
    
        /* put in the data  */
        new_node->data  = new_data;
    
        /* link the old list off the new node */
        new_node->next = (*head_ref);    
    
        /* move the head to point to the new node */
        (*head_ref)    = new_node;
    }
    
    /* Function to print linked list */
    void printList(struct node *node)
    {
        while(node != NULL)
        {
            printf("%d  ", node->data);
            node = node->next;
        }
    }    
    
    /* Drier program to test above function*/
    int main(void)
    {
        /* Start with the empty list */
        struct node* head = NULL;
    
         /* Created Linked list is 1->2->3->4->5->6->7->8 */
         push(&head, 8);
         push(&head, 7);
         push(&head, 6);
         push(&head, 5);
         push(&head, 4);
         push(&head, 3);
         push(&head, 2);
         push(&head, 1);           
    
         printf("\n Given linked list \n");
         printList(head);
         head = reverse(head, 3);
    
         printf("\n Reversed Linked list \n");
         printList(head);
    
         getchar();
         return(0);
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Linked List Deletion
    By TopGun in forum Linked Lists
    Replies: 1
    Last Post: 30th April 2011, 14:28
  2. Reverse K nodes of a List
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 3
    Last Post: 23rd April 2010, 00:35
  3. Linked List merge
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 30th May 2008, 15:22
  4. Linked List Reversal
    By TopGun in forum Linked Lists
    Replies: 0
    Last Post: 17th February 2008, 17:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts