Thursday, April 24, 2014

Copy a Linked List in C

Node Copy(Node first)
{
   if(first == NULL)
      return NULL;
   Node copy = (Node)malloc(sizeof(Node));
   Node temp = copy;

   while(first->next != NULL) {
      temp->data = first->data;
      temp->next = (Node)malloc(sizeof(Node));
      temp = temp->next;
      first = first->next;
   }
   temp->data = first->data;
   return copy; 
}

No comments:

Post a Comment