Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
The code for the Copy method of the LinkedList class is given in Program . The Copy method is used to assign the elements of one list to another. It does this by discarding the current list elements and then building a copy of the given linked list.
Program: LinkedList class Copy method.
The Copy method begins by calling Purge to make sure that the list to which new contents are being assigned is empty. Then, it traverses the list passed to it one-by-one calling the Append method to append the items to the list begin constructed.
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).