|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Python |
The code for the __copy__ method
of the LinkedList class is given in Program
.
The __copy__ method is used
to create a shallow copy of a given list.

Program: LinkedList class __copy__ method.
The __copy__ method first creates a new empty LinkedList instance. Then, it traverses the elements of the given list one-by-one calling the append method to append the items to the new list.
In Section
the running time for the append
method was determined to be O(1).
If the resulting list has n elements,
the append method will be called n times.
Therefore, the running time of the __copy__ method is O(n).