Skip to content

Instantly share code, notes, and snippets.

@tedicela
Created November 15, 2019 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedicela/09c00fb208c3d70ac1806ac994bc7d9c to your computer and use it in GitHub Desktop.
Save tedicela/09c00fb208c3d70ac1806ac994bc7d9c to your computer and use it in GitHub Desktop.
Reversing a Singly LinkedList
<?php
function reverseLinkedList($l){
if($l == null){
return $l;
}
$reverse = clone $l;
$reverse->next = null;
while($l->next != null){
$l = $l->next;
$tmp = clone $l;
$tmp->next = $reverse;
$reverse = $tmp;
unset($tmp);
}
return $reverse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment