Skip to content

Latest commit

 

History

History
34 lines (24 loc) · 842 Bytes

File metadata and controls

34 lines (24 loc) · 842 Bytes
 Node* pairWiseSwap(struct Node* head) 
    {
        if(head==NULL || head->next==NULL) return head;
        
        Node* a=head;
        Node* start=NULL;
        Node* prlast=NULL;
        
        while(a!=NULL && a->next!=NULL)
        {
        
            Node* b=a->next;
            a->next=b->next;
            b->next=a;
           // cout<<"a: "<<a->data<<" b: "<<b->data<<endl;
            
            
            if(a==head) 
                start=b;
                
            if(prlast!=NULL)
                prlast->next=b;
            
           
            prlast=a;
            a=a->next;
        }
        
        return start;
    }