/** * @param DoubleLinkedList $first * @param DoubleLinkedList $second * @return DoubleLinkedListNode|DoubleLinkedList */ private function append(DoubleLinkedList $first, DoubleLinkedList $second) { $listToReturn = new DoubleLinkedList(); if ($first->isEmpty()) { $listToReturn = $second; } elseif ($second->isEmpty()) { $listToReturn = $first; } else { /** @var DoubleLinkedListNode $bottom */ $bottom = $second->getBottom(); $bottom->setPrev($first->getTop()); $first->getTop()->setNext($second->getBottom()); $listToReturn->setBottom($first->getBottom()); $listToReturn->setTop($second->getTop()); } return $listToReturn; }