Given a linked list, reverse alternate nodes and append at the end

PROBLEM :

Given a linked list,performs the following task

Remove alternative nodes from second node
Reverse the removed list.
Append the removed list at the end.
Input :

You have to complete the method which takes one argument: the head of the linked list. You should not read any input from stdin/console.
The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list.
There are multiple test cases. For each test case, this method will be called individually.

Output:

You should not print any output to stdin/console


Example:

Input List: 1->2->3->4->5->6
After step 1 Linked list are 1>3->5 and 2->4->6
After step 2 Linked list are 1->3->5 abd 6->4->2
After step 3 Linked List is 1->3->5->6->4->2
Output List: 1->3->5->6->4->2

Note: If you use "Test" or "Expected Output Button" use below example format

INPUT:
1(Number of Test cases)
8
10 4 9 1 3 5 9 4

OUTPUT:
10 9 3 9 4 5 1 4

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*
  reverse alternate nodes and append at the end
  The input list will have at least one element
  Node is defined as
  struct node
  {
     int data;
     struct node *next;
  }

*/
struct node* reverse(struct node *) ;

void rearrange(struct node *odd)
{
    struct node *one,*two,*temp,*ptr ;
    one=NULL ;
    two=NULL ;
   
    temp=odd ;
    bool swap=true ;
   
    while(temp!=NULL)
    {
        if(swap)
        {
            if(one==NULL)
            {
                one=temp ;
                temp=temp->next ;
                one->next=NULL;
                swap=!swap ;
            }
            else
            {
                ptr=one ;
                while(ptr->next!=NULL)
                    ptr=ptr->next ;
                ptr->next=temp ;
                temp=temp->next ;
                ptr->next->next=NULL ;
                swap=!swap ;
            }
        }
        else
        {
           if(two==NULL)
            {
                two=temp ;
                temp=temp->next ;
                two->next=NULL;
                swap=!swap ;
            }
            else
            {
                ptr=two ;
                while(ptr->next!=NULL)
                    ptr=ptr->next ;
                ptr->next=temp ;
                temp=temp->next ;
                ptr->next->next=NULL ;
                swap=!swap ;
            }
        }
    }
   
    two=reverse(two) ;
   
    ptr=one ;
    while(ptr->next!=NULL)
        ptr=ptr->next ;
   
    ptr->next=two ;
   
   
}

struct node* reverse(struct node *head)
{
    if(head==NULL)
    return head ;
   
    if(head->next==NULL)
    return head;
   
    struct node *one,*two,*third ;
    one=head ;
    two=one->next ;
    third=two->next ;
   
    one->next=NULL ;
    two->next=one ;
   
    while(third!=NULL)
    {
        one=two ;
        two=third ;
        third=third->next ;
       
        two->next=one ;
    }
    return two ;
   
}


---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )