1. <?php
    2. Class ListNode {
    3. public $val = 0;
    4. public $next;
    5. public function __construct($val) {
    6. $this->val = $val;
    7. }
    8. public function __get($name) {
    9. return null;
    10. }
    11. }
    12. class Solution {
    13. public function removeNthFromEnd(ListNode $head, $n) {
    14. $dummy = new ListNode(0);
    15. $dummy->next = $head;
    16. $fastP = $dummy;
    17. $slowP = $dummy;
    18. for ($i = 0; $i <= $n; $i++) {
    19. $fastP = $fastP->next;
    20. }
    21. while ($fastP->next) {
    22. $fastP = $fastP->next;
    23. $slowP = $slowP->next;
    24. }
    25. $slowP->next->next = $fastP;
    26. return $dummy->next;
    27. }
    28. }
    29. $head = new ListNode(1);
    30. $head->next = new ListNode(2);
    31. $head->next->next = new ListNode(3);
    32. $head->next->next->next = new ListNode(4);
    33. $head->next->next->next->next = new ListNode(5);
    34. $cls = new Solution();
    35. $ret = $cls->removeNthFromEnd($head, 2);
    36. print_r($ret);