Ronzii's Blog

Just your average geek's blog

Given a single linked list, A->B->C->D->E->F, convert this to B->A->D->C->F->E…

struct node
{
    struct node* next;
    char data;
};
typedef struct node Node;
Node* altSwap(Node* current)
{
    Node* head = current;
    Node* temp;
    Node* previous=NULL;
    while(current && current->next)
    {
        temp = current->next;
        if(current==head)
        {
            head =temp;
        }
        current->next = temp->next;
        temp->next = current;
        if(previous!=NULL)
            previous->next = temp;
        previous = current;
        current = current->next;
    }
    return head;
}
Advertisements

March 8, 2012 Posted by | C++ | , , , , | Leave a comment