List copy semantics efficiency

This is a discussion on List copy semantics efficiency within the C++ Programming forums, part of the General Programming Boards category; This never dawned on me before:
Typically ( as far as I know ) people handle linked-list copying by iterating ...

2) Wouldn't iterating the list once and pushing the front, then reversing the order be more efficient ( because isnt' that O( 2 * N )? )

It's just O(N). O(2*N) is the same as O(N).

Anyway, you're assuming that pushing at the back of the list is inefficient. This only requires a list traversal if you do it naively. If instead, you maintain a pointer/iterator to the end of the list, you can insert each element in O(1) time, avoiding O(N^2) and also getting rid of the 2x constant.

In theory, a specialization of std::back_insert_iterator<std::list<T> > could be made to do this. Such a specialization may already exist.

What for? std::list is a doubly linked list and maintains a reference to the back already. Its push_back() is O(1).

std::forward_list (coming in C++09) does not provide push_back, though. That's because of the design principle that there's zero overhead compared to a hand-written singly linked list, and a pointer to the last element would incur such overhead.

What for? std::list is a doubly linked list and maintains a reference to the back already. Its push_back() is O(1).

std::forward_list (coming in C++09) does not provide push_back, though. That's because of the design principle that there's zero overhead compared to a hand-written singly linked list, and a pointer to the last element would incur such overhead.

I'd say that it's more likely that it doesn't hold a pointer to the last element because it takes O(n) time to update it when you delete the last item.

I do not have time and the code is missing now, after reinstallation of XP, but adding to a singly linked list can be achieved in O(1). Instead of iterating over the list from start to end, to find where to append new node, just keep the last added node saved.

I do not have time and the code is missing now, after reinstallation of XP, but adding to a singly linked list can be achieved in O(1). Instead of iterating over the list from start to end, to find where to append new node, just keep the last added node saved.

Yes, the debate is about deleting the last node, which requires traipsing through the list to find the previous to last node.

It takes O(n) time to delete the last item in the first place, and in the course of that you can adjust the pointer at no additional complexity cost. So no, that's not it.

Sure, if you pretend there are no such things as iterators.

If you have an iterator pointing to the last element then you could not delete the item it points to AND correctly update the ltail pointer in anything less than O(n) time. That's on top of the fact that iterating through the list is also O(n). Therefore if you were to iterate over the list backwards, deleting as you go, it would be O(n*n).
I'm pretty sure this would be the main reason why there is no tail pointer.