C program to make a second copy of a linked list

前端 未结 3 1355
滥情空心
滥情空心 2021-01-05 22:01

I was writing a C code to copy the contents of a Linked List onto another list. I want to know if there is a more efficient way of doing this.

Which is better?

3条回答
  •  清酒与你
    2021-01-05 22:20

    Without going recursive, this is about the most compact you can get:

    struct node *copy(struct node *org)
    {
    struct node *new=NULL,**tail = &new;
    
    for( ;org; org = org->link) {
        *tail = malloc (sizeof **tail );
        (*tail)->info = org->info;
        (*tail)->link = NULL;
        tail = &(*tail)->link;
        }
    return new;
    }
    

提交回复
热议问题