Building circular linked list
Building a circular linked list is not at as hard as it sounds from the name. So far, we have seen that adding a new node at the end is pretty simple; we set the next reference of the last node to NULL
. In a circular linked list, the last node's next reference will actually point to the first node, thereby creating a circular list. Let's write a simple circular linked list where the nodes will be inserted at the end of the list:
class CircularLinkedList { private $_firstNode = NULL; private $_totalNode = 0; public function insertAtEnd(string $data = NULL) { $newNode = new ListNode($data); if ($this->_firstNode === NULL) { $this->_firstNode = &$newNode; } else { $currentNode = $this->_firstNode; while ($currentNode->next !== $this->_firstNode) { $currentNode = $currentNode->next; } $currentNode->next = $newNode...