I this this code will solves it ...

*#include <stdio.h>
#include <stdlib.h>

struct node{
    int val;
    struct node *next;
};

int main(){

    struct node *head = NULL , *next_ptr = head;
    int n = 1;

    while ( n <= 6 ){
        struct node *temp = (struct node*) malloc (sizeof(struct node) );
        temp->val = n++;
        temp->next = NULL;

        if (head == NULL ){
            head = temp ;
            next_ptr = head;
        }else{
            next_ptr->next = temp;
            next_ptr = temp;
        }

    }

    next_ptr = head;
    head = NULL;
    struct node *prev = NULL;

    while ( next_ptr != NULL && next_ptr->next != NULL ){
        struct node *swp = next_ptr->next->next;

        if ( head == NULL){
            head = next_ptr->next;
        }


        if ( prev == NULL ){
            prev = next_ptr;
        }else{
            prev->next = next_ptr->next;
            prev = next_ptr;
        }

        next_ptr->next->next = next_ptr;
        next_ptr->next = swp;
        next_ptr = swp;

    }


    while ( head != NULL ){
        printf("%d \n" , head->val);
        head = head->next;
    }

    return 0;
}
*





On Thu, Jun 2, 2011 at 7:18 AM, Anurag Bhatia <[email protected]> wrote:

> Have a look at this recursive soln in java -
>
> Node convertList(Node head)
>        {
>                if(head ==null)
>                        return null;
>
>                Node node = head;
>                Node next = node.next;
>                Node next2next = next.next;
>
>                next.next = node;
>                node.next = convertList(next2next);
>
>                return next;
>        }
>
> This soln assumes even number of nodes in the list. The code is quite
> self explanatory.
>
> --Anurag
>
>
> On Wed, Jun 1, 2011 at 6:26 AM, Anand <[email protected]> wrote:
> > Given a linked list of the form, 1->2->3->4->5->6, convert it into the
> form
> > 2->1->4->3->6->5. Note that the nodes need to be altered and not the data
> > contained in them
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/algogeeks?hl=en.
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
ROHIT SINDHU
B.Tech (IT) -- 6th sem.
INDIAN INSTITUTE OF INFORMATION TECHNOLOGY, ALLAHABAD.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to