From 3aaa5dffbbe09016300784950cc857647cf0c926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Chac=C3=B3n=20Guti=C3=A9rrez?= <138903866+joseantoniochacon@users.noreply.github.com> Date: Sat, 1 Jun 2024 13:13:05 -0600 Subject: [PATCH] Time: 12 ms (13.91%), Space: 19.8 MB (87.83%) - LeetHub --- .../0876-middle-of-the-linked-list.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.php diff --git a/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.php b/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.php new file mode 100644 index 0000000..160c227 --- /dev/null +++ b/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.php @@ -0,0 +1,32 @@ +/** + * Definition for a singly-linked list. + * class ListNode { + * public $val = 0; + * public $next = null; + * function __construct($val = 0, $next = null) { + * $this->val = $val; + * $this->next = $next; + * } + * } + */ +class Solution { + + /** + * @param ListNode $head + * @return ListNode + */ + function middleNode($head) { + $size = 0; + $navigationNode = $head; + while($navigationNode != null){ + $navigationNode = $navigationNode->next; + $size++; + } + // Dividing by 2 without round + $size = bcdiv($size, 2, 0); + for($i=0;$i<$size;$i++){ + $head = $head->next; + } + return $head; + } +} \ No newline at end of file