@Greg, I took your code and made it even shorter :)
public static Node Duplicate(Node n)
{
if (n == null) return null;
Node first = new Node();
Node current = first;
do
{
current.Data = n.Data;
current = (current.Next = new Node());
n = n.Next;
} while (n != null)
return first;
}
The Do-While construct is often forgotten, but fits well here.
The Node.Clone () method will also be nice.
+1 to Greg for a good example.
source
share