c - Create duplicate Linked List with given Linked list -


problem given single (given : head, last element->next = null) linked list next pointer , random pointer attribute of ll node.

struct node {   node *next;   node *random; } 

now have duplicate ll (only c code)

i giving straight forward solution copy linked list node node.

let have linked list this, head -> node1 -> node2 -> ... noden -> null.

struct node * head_dup = null; //create head of duplicate linked list. struct node * tmp1 = head, * tmp2 = head_dup; //tmp1 traversing original linked list , tmp2 building duplicate linked list. while( tmp1 != null) {     tmp2 = malloc(sizeof(struct node)); //allocate memory new node in duplicate linked list.     tmp2->random = malloc(sizeof(struct node)); //not sure storing here allocate memory , copy content of tmp1.     *(tmp2->random) = *(tmp1->random);     tmp2->next = null; //assign null @ next node of duplicate linked list.     tmp2 = tmp2->next; //move both pointers point next node.      tmp1 = tmp1->next; } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -