Singly-linked list/Reversal: Difference between revisions
Content deleted Content added
Added FreeBASIC |
|||
Line 74:
=={{header|Common Lisp}}==
Common Lisp has functions for reversing a
A non-destructive reversal using dolist:
Line 128:
until (endp cur)
do (setf (cdr cur) prev)
finally (return prev)))
</syntaxhighlight>
Here is a slightly shorter way to define a destructive linked list reversal function without relying on tail recursion or the fact that (cdr nil) is nil (assuming the argument is proper without checking):
<syntaxhighlight lang="Lisp">
(defun my-nvreverse (list)
(loop with prev = nil
until (null list)
do (rotatef (cdr list) prev list)
finally (return prev)))
</syntaxhighlight>
|