0

Is this code good to clone a linked list?


public linkedList clone()
        {
            linkedList lst1 = new linkedList();
            Node tmp = head;

            for (int i = 0; i <= count; i++)
            {
                lst1.Add(tmp.data);
                tmp = tmp.next;
            }
            return lst1;
        }

also can you guys suggest any other easy way to clone a linked list

  • Did this code work for you? – Chetan Jun 25 '22 at 09:37
  • 1
    `i <= count` is unlikely to be correct... and I'd strongly advise following normal .NET naming conventions, where type, method and property names are in Pascal Case (so `LinkedList` rather than `linkedList`, `Clone` rather than `clone`, and `Next` rather than `next`). I'd expect a linked list to implement `IEnumerable` (or better yet `IEnumerable`) and that would be a simpler way of cloning it, to be honest... – Jon Skeet Jun 25 '22 at 09:40
  • check this answer [How do I clone a generic list in C#?](https://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c?rq=1) – YoussefSell Jun 25 '22 at 13:14

0 Answers0