Singly-linked list/Reversal: Difference between revisions

(Added FreeBASIC)
 
Line 74:
 
=={{header|Common Lisp}}==
Common Lisp has functions for reversing a listsequences in its standard library, which includes, but is not limited to, linked list reversal. The destructive version is called nreverse, and the version that makes a reversed copy of the list is called reverse. However, it's simple to make your own versions of these functions. Here is a simplified version that only reverses lists:
 
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>