Data Structures, Algorithms, & Applications in Java
Chapter 6, Exercise 53
Two doubly-linked circular lists with header nodes may be joined in
O(1) time as below.
public class HeadDoubleCircularListWithJoin
extends HeadDoubleCircularList
{
/** append list a to this
* following the join, a is empty */
public void join(HeadDoubleCircularListWithJoin a)
{
// special cases
if (a.size == 0)
// a is empty, nothing to join to this
return;
// link nodes of list a (other than its header) into end of this
DoubleNode lastNode = headerNode.previous;
DoubleNode aFirstNode = a.headerNode.next;
DoubleNode aLastNode = a.headerNode.previous;
lastNode.next = aFirstNode;
aFirstNode.previous = lastNode;
aLastNode.next = headerNode;
headerNode.previous = aLastNode;
size += a.size;
// make a empty
a.clear();
return;
}
}