19. Remove Nth Node From End of List
/**
* 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
* @param Integer $n
* @return ListNode
*/
function removeNthFromEnd($head, $n) {
$start = new ListNode(0, $head);
$slow = $start;
$fast = $start;
while($fast && $fast->next) {
if ($n != 0) {
--$n;
$fast = $fast->next;
} else {
$fast = $fast->next;
$slow = $slow->next;
}
}
$slow->next = $slow->next->next ?? null;
return $start->next;
}
}
Last updated