delete last node of a circular linked list

can i delete last node(that points to the first node) of the circular linked list in constant time?
If you mean constant time as the time it takes to delete an element of a list independent of its size, then i think you can since you have a pointer to the last element, and no iteration is necessary.
If I follow your method, then how will I make the second-last node point to first node?
Lets call the target node to delete (the last one): discard

1
2
3
4
5
6
7
8
9
// Set pointers to the previous and next nodes of discard
NodePtr* prev = discard -> getPreviousLink( );
NodePtr* next = discard -> getNextLink( );

// Bypass discard, connect the two nodes (in this case the second-last and first)
prev -> setNextLink(next);
next -> setPreviousLink(prev);

delete discard;


This can be done in constant time since you can simply find the last node by getting the previous link of the first node.
Topic archived. No new replies allowed.