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:
- 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.
Iterate through existing list for each node
if node next is not null then
Execute step 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.