DEV Community

Dhanashree Rugi
Dhanashree Rugi

Posted on

1 1

Create a Copy of a Linked List

For a given linked list, you are supposed to write a function that creates a copy of the already created linked list.

Example 1:
Input : Linked List : 4 6 2 8 1
Output : Copy of linked list : 4 6 2 8 1

Example 2:
Input : Linked List : 4 5 9 8 2
Output : Copy of linked list : 4 5 9 8 2

Explanation:

  • Logic behind this is just create a new node of structure and copy the every existing node to new node.
  • Process will continue until the existing list leads to null(recursively).

C program that creates the copy of linked list:

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

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

void displayLL(struct node * head)
{
    struct node * temp;
    temp = head;
    temp=head;
    while(temp!=0)
    {
       printf("%d ",temp->data);
       temp = temp->next;
    }
}

void copy(struct node *head)
{
    struct node *temp = head;
    struct node *temp1, *temp2, *head1;

    temp1 = (struct node *)malloc(sizeof(struct node));
    temp1->next = NULL;
    temp1->data = temp->data;
    head1 = temp1;
    temp1->next = NULL;
    temp = temp->next;

    while(temp != NULL)
    {
       temp2 = (struct node *)malloc(sizeof(struct node));
       temp2->data = temp->data;
       temp1->next = temp2;
       temp1 = temp2;
       temp = temp->next;
    }
    printf("\n--------------------------------\n");
    printf("Copy of original linked list : ");
    displayLL(head1);
}

int main()
{
   struct node *head = 0, *newnode, *temp; 
   int n, choice, newdata;

// Create Linked List // 

   printf("Enter the number of nodes in the list : ");
   scanf("%d", &n);

   if(n == 0)
   {
      printf("--------------------------------\n");
      printf("Linked list cannot be empty");
      exit(0);
    } 

   for(int i = 1; i<=n; i++)
   {
     newnode = (struct node *)malloc(sizeof(struct node));
     printf("Enter the data%d : ", i);
     scanf("%d", &newnode->data);
     newnode->next = 0;
     if(head == 0)
     {
        head = temp = newnode;
     } 
     else
       { 
        temp->next = newnode;
        temp = newnode;
       }
    }
   printf("--------------------------------\n");
   printf("Original linked list : ");
   displayLL(head);
   copy(head);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Enter the number of nodes in the list : 4
Enter the data1 : 3
Enter the data2 : 7
Enter the data3 : 3
Enter the data4 : 8
--------------------------------
Original linked list : 3 7 3 8 
--------------------------------
Copy of original linked list : 3 7 3 8 
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay