private void insertNodes ( Node node , Node newNode ) { // Keep a reference to the next node in the node's chain as this will be overwritten when attaching the node // or chain into the root list. Node oldNodeNext = newNode . next ; // Break open the node's chain and attach it into the root list. newNode . next . prev = node ; newNode . next = node . next ; // Break open the root list chain and attach it to the new node or chain. node . next . prev = newNode ; node . next = oldNodeNext ; }
Inserts a single node or a circular doubly linked list of nodes into a list next to the specified node . I does not matter if the specified nodes are singletons or part of a chain as they will be correctly linked in in either case so long as their prev and next references form a loop with themselves .