Quick actions

cmd+k|ctrl+k

Navigation

Languages

reversing only half of the linked list

Snippet info

Language

Cpp

Visibility

public

Author

gsubhikaran

Created

2019-06-15T09:21:21Z

Updated

2019-06-15T09:21:21Z

#include <iostream>
using namespace std;
struct node{
    int data;
    node *next;
};

node *head3=NULL;
node *head4=NULL;


node* push(node *head,int data)
{
    node * temp=head;
    node * newN=new node;
    newN->next=NULL;
    newN->data=data;
    if(temp==NULL)
    return newN;
    while(temp->next!=NULL)
        temp=temp->next;
    temp->next=newN;
    return head;
}
void display(node *temp)
  {
      node *dup=temp;
      while(dup!=NULL)
      {
          cout<<dup->data<<' ';
          dup=dup->next;
      }
      
      
  }
  void reverse(node *head)
   {
       if(head==NULL)
       return;
       reverse(head->next);
       head3=push(head3,head->data);
       
   }
   void merge()
   {
       node *temp=head4;
       while(temp->next!=NULL)
        {
            temp=temp->next;
        }
     temp->next=head3;    
       
   }

int main() {
    int n,x;
    cin>>n;
    int i;
    node *head=NULL;
    node *head2=NULL;
     
    for(i=1;i<=n;i++)
    {  
        cin>>x;
     head=push(head,x);
     if(i<=n/2)
     head4=push(head4,x);
     if(i>n/2)
    head2=push(head2,x);
    }
    reverse(head2);
    merge();
    display(head4);
    

    for(i=1;i<=n/2;i++)
    
    
    
    return 0;
}
INFO