Skip to content

Commit

Permalink
Time: 12 ms (13.91%), Space: 19.8 MB (87.83%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
joseantoniochacon committed Jun 1, 2024
1 parent 15817be commit 3aaa5df
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 3aaa5df

Please sign in to comment.