Copy a linked list

前端 未结 3 1503
南方客
南方客 2021-01-01 19:48
typedef struct Node
{
  int data;
  Node *next;
  Node *other;
};

Node *pHead;

pHead is a singly linked list. The next f

3条回答
  •  春和景丽
    2021-01-01 20:03

    Came across this. Hope it helps!

    Citing one solution from this link, below.

    1) Create the copy of 1 and insert it between 1 & 2, create the copy of 2 and insert it between 2 & 3.. Continue in this fashion, add the copy of N to Nth node

    2) Now copy the arbitrary link in this fashion

     if original->arbitrary is not NULL
       original->next->arbitrary = original->arbitrary->next;  /*TRAVERSE TWO NODES*/
     else
       original->next->arbitrary=NULL;
    

    This works because original->next is nothing but copy of original and Original->arbitrary->next is nothing but copy of arbitrary.

    3) Now restore the original and copy linked lists in this fashion in a single loop.

     original->next = original->next->next;
     copy->next = copy->next->next;
    

    4) Make sure that last element of original->next is NULL.

    Sample code, Time Complexity O(N), Space Complexity O(1)

    pNode copy_list(pNode head) {
      // pre-condition: node->other either points into the list or NULL
      if (!head) return NULL;
    
      pNode node = head, copied = NULL, cnode = NULL;
      for ( ; node; node = node->next->next) {
        // make copy
        cnode = newnode(node->next, node->data);
        cnode->other = node->other;
        if (node == head)
          copied = cnode;
    
        // insert the copy between originals    
        node->next = cnode;    
        // node -> cnode -> (orig)node->next
      }
    
      for (node = head; node && node->next; 
           node = node->next->next /* only original nodes */) 
        if (node->other)
          node->next->other = node->other->next;
        else
          node->next->other = NULL;    
    
      // restore lists
      node = head; cnode = copied;
      for ( ; cnode && cnode->next; node = node->next, cnode = cnode->next) { 
        node->next = node->next->next;
        cnode->next = cnode->next->next;
      }
      node->next = NULL;
      return copied;
    }
    

    Complete program is at http://gist.github.com/349630

提交回复
热议问题