|
26 | 26 | namespace doganoo\PHPAlgorithms\Common\Abstracts; |
27 | 27 |
|
28 | 28 | use doganoo\PHPAlgorithms\Common\Interfaces\IComparable; |
| 29 | +use doganoo\PHPAlgorithms\Common\Interfaces\INode; |
29 | 30 | use doganoo\PHPAlgorithms\Common\Interfaces\IUnaryNode; |
30 | 31 | use doganoo\PHPAlgorithms\Common\Util\Comparator; |
31 | 32 | use doganoo\PHPAlgorithms\Datastructure\Lists\Node; |
@@ -285,27 +286,6 @@ public abstract function prepend(?Node $node): bool; |
285 | 286 | // return $list; |
286 | 287 | //} |
287 | 288 |
|
288 | | - /** |
289 | | - * returns the number of elements in a list |
290 | | - * |
291 | | - * @return int |
292 | | - */ |
293 | | - public function size() { |
294 | | - if ($this->isEmpty()) { |
295 | | - return 0; |
296 | | - } |
297 | | - return $this->head->size(); |
298 | | - } |
299 | | - |
300 | | - /** |
301 | | - * if the list is empty or not |
302 | | - * |
303 | | - * @return bool |
304 | | - */ |
305 | | - public function isEmpty() { |
306 | | - return $this->head == null; |
307 | | - } |
308 | | - |
309 | 289 | /** |
310 | 290 | * adds a Node instance to the list |
311 | 291 | * |
@@ -537,6 +517,76 @@ public function getMiddleNode(): ?IUnaryNode { |
537 | 517 | return $p; |
538 | 518 | } |
539 | 519 |
|
| 520 | + public function getIntersectionNode(AbstractLinkedList $list): ?INode { |
| 521 | + if (0 === $this->size()) return null; |
| 522 | + if (0 === $list->size()) return null; |
| 523 | + |
| 524 | + $l1 = $this->getHead(); |
| 525 | + $l2 = $list->getHead(); |
| 526 | + |
| 527 | + $c1 = 0; |
| 528 | + $c2 = 0; |
| 529 | + |
| 530 | + while (null !== $l1) { |
| 531 | + $c1++; |
| 532 | + $l1 = $l1->getNext(); |
| 533 | + } |
| 534 | + |
| 535 | + while (null !== $l2) { |
| 536 | + $c2++; |
| 537 | + $l2 = $l2->getNext(); |
| 538 | + } |
| 539 | + |
| 540 | + $l1 = $this->getHead(); |
| 541 | + $l2 = $list->getHead(); |
| 542 | + |
| 543 | + if ($c1 > $c2) { |
| 544 | + $len = $c1 - $c2; |
| 545 | + while ($len > 0) { |
| 546 | + $l1 = $l1->getNext(); |
| 547 | + $len--; |
| 548 | + } |
| 549 | + } else { |
| 550 | + $len = $c2 - $c1; |
| 551 | + while ($len > 0) { |
| 552 | + $l2 = $l2->getNext(); |
| 553 | + $len--; |
| 554 | + } |
| 555 | + } |
| 556 | + |
| 557 | + |
| 558 | + while (null !== $l1 && null !== $l2) { |
| 559 | + if ($l1 == $l2) { |
| 560 | + return $l1; |
| 561 | + } |
| 562 | + $l1 = $l1->getNext(); |
| 563 | + $l2 = $l2->getNext(); |
| 564 | + } |
| 565 | + |
| 566 | + return null; |
| 567 | + } |
| 568 | + |
| 569 | + /** |
| 570 | + * returns the number of elements in a list |
| 571 | + * |
| 572 | + * @return int |
| 573 | + */ |
| 574 | + public function size() { |
| 575 | + if ($this->isEmpty()) { |
| 576 | + return 0; |
| 577 | + } |
| 578 | + return $this->head->size(); |
| 579 | + } |
| 580 | + |
| 581 | + /** |
| 582 | + * if the list is empty or not |
| 583 | + * |
| 584 | + * @return bool |
| 585 | + */ |
| 586 | + public function isEmpty() { |
| 587 | + return $this->head == null; |
| 588 | + } |
| 589 | + |
540 | 590 | /** |
541 | 591 | * Specify data which should be serialized to JSON |
542 | 592 | * |
|
0 commit comments