linked list를 node 두 개씩 거꾸로 바꾸기 본문

Programming/Questions

linked list를 node 두 개씩 거꾸로 바꾸기

halatha 2008. 9. 2. 14:55

http://www.careercup.com/question?id=58132

1. Implement memcopy considering the overlap
2. Given a linked list:
a->b->c->d....
Write a function to swap the each node pair , such that output will be as follows:
b->a->d->c->...

void reversePair(Node** p_ppstrHead)
{
    Node*    z_pstr1st = *p_ppstrHead;
    Node*    z_pstr2nd = (*p_ppstrHead)->next;
    Node*    z_pstr3rd = z_pstr1st;

    *p_ppstrHead = z_pstr2nd;

    while ( z_pstr1st != NULL && z_pstr2nd != NULL )
    {
        z_pstr1st->next = z_pstr2nd->next;
        z_pstr2nd->next = z_pstr1st;

        if ( z_pstr1st->next != NULL )
        {
            z_pstr1st = z_pstr1st->next;
        }

        if ( z_pstr1st != NULL )
        {
            z_pstr2nd = z_pstr1st->next;
            if ( z_pstr2nd )
            {
                z_pstr3rd->next = z_pstr2nd;
                z_pstr3rd = z_pstr1st;
            }
        }
    }
}
Comments