Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

__copy__ Method

The code for the __copy__ method of the LinkedList class is given in Program gif. The __copy__ method is used to create a shallow copy of a given list.

   program3970
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 gif 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).


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.