-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 12 ms (13.91%), Space: 19.8 MB (87.83%) - LeetHub
- Loading branch information
1 parent
15817be
commit 3aaa5df
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |