# List reversing

There is a recursive method:

```(nil  defmethod rev (result)  result)
('cons  defmethod rev (result)
((this  rest) rev (nil  cons  (this first)  result)))```

In these simple tasks is unprofitable to use recursion, because the result is the excessive use of stack memory for the return of function. Use the variable.

```(nil  defmethod rev ()
(nil  let (result (i  this))
(nil  while (nil  consp i)
(nil  setq  result  (nil  cons  (i  first)  result) i (i  rest)))
result))```

Lists may not be linear and looped. In such circumstances, the program will loop so as not to be overseen by the possibility of cycles. The complexity of the algorithm will be O(n²). The result will be a list without a cycle.

```(nil  defmethod rev ()
(nil  let ((i this) (next (this rest))  result  (flagcycled false))
(nil  while (nil  and (nil  consp i)  (nil  not flagcycled))
(nil  setq  result  (nil  cons  (i  first)  result))
(nil  if  (next find  this  i)
(nil  setq  flagcycled  true)
(nil  setq  i next  next  (next rest))))
result))
(nil  defmethod find  (first  last)
(nil  let ((flagfound false))
(nil  while (nil  and (nil  not (nil  eq  first last))  (nil  not flagfound))
(nil  if  (nil  eq  this  first)
(nil  setq  flagfound true)
(nil  setq  first (first  rest))))
(nil  if  (nil  not flagfound)
(nil  if  (nil  eq  this  first)
(nil  setq  flagfound true)))
flagfound))```

Using algorithms, the complexity of O(n²) is a restricted area for programmers. Such situations are avoided. Rotating list is essentially a graph (math.). The graph has no concept of reversing.