top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to clone each node of given linked list using C?

+1 vote
522 views
How to clone each node of given linked list using C?
posted May 6, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Cloning of a linked list means create a linked list from an existing linked which would have same node values in the same order.
I would like to share the algorithm as following:

Prerequisites:
- Source linked list pointer
- Head node pointer for clone list
- Last pointer, pointing to head node. This pointer points to the last node after insertion of new node.

  1. Iterate through existing list for each node
    if node next is not null then
    Execute step 2.
    else
    Exit.

  2. Allocate a node, assign the value to node from source node and insert this new node to target list just after the last node.
    Move the last pointer to this new node of target list.

answer May 6, 2017 by Harshita
...