How to Reverse a List

How do I reverse a list or loop over it backwards?

To get a new reversed list, apply the reversed function and collect the items into a list:

>>> xs = [0, 10, 20, 40]
>>> list(reversed(xs))
[40, 20, 10, 0]

To iterate backwards through a list:

>>> xs = [0, 10, 20, 40]
>>> for x in reversed(xs):
... print(x)
40
20
10
0

How to reverse list in Scheme?

It's very rare to use the "back end" of a list for anything, it's both inefficient and tends to cause rather complex code (as you've noticed).

In order to reverse a list, you can save the first element, reverse the rest of it, and then put the old first element at the back of the "reversed rest".

(This is the same as what you're doing, but at the other end of the list.)

That is,

(define (reverse lst)
(if (null? lst)
lst
(append (reverse (cdr lst)) (list (car lst)))))

This is quite inefficient though, so normally you would use a tail-recursive version ("iterative process" in SICP).

A tail-recursive implementation is perhaps most easily understood if you decompose it into a main function and a "helper":

(define (reverse-helper lst acc)
(if (null? lst)
acc
(reverse-helper (cdr lst) (cons (car lst) acc))))

(define (reverse lst)
(reverse-helper lst '()))

The main difference is that building the result in the acc parameter means that we can use cons and not need to repeatedly traverse the result to add things at the back of it (which is what append does).

Reverse list method print None for a list returned in a function?

list.reverse() mutates the list in-place and returns None. reverse leaves the original list alone and returns a reversed version of it.

How to reverse a list of lists in python?

In [24]: L = [[1,2,3],[4,5,6],[7,8,9]]

In [25]: L[::-1]
Out[25]: [[7, 8, 9], [4, 5, 6], [1, 2, 3]]

How to reverse a linked list implementation of a queue

First of all, you have a problem in this line:

    node *node = (node*)malloc(sizeof(node));

As you shadow the type node by the pointer with the same name, the argument given to sizeof is wrong. Use a different variable name in that function.

Also, in C it is considered better not to cast what malloc returns:

    node *curnode = malloc(sizeof(node));
// ...use curnode below ...

The reverse function

The problem is with this line:

q->head = node;

As you have just reversed the queue that starts at node->next, node->next will now be the tail of the queue, so when you do the above assignment, you limit the queue to two elements, and lose the reference to the other nodes.

As a solution, don't use a second function argument, nor next references. Just dequeue - recurse - and enqueue again:

void reverse(queue *q)
{
if (empty(q))
return;
int tmp = dequeue(q);
reverse(q);
enqueue(q, tmp);
}

How to reverse a list without modifying the original list in Python

you need to make a copy of your list

L=[1,2,3]
# R=L
R = L[:]
L.reverse()

or more directly (reversing using slice notation):

R = L[::-1]

if you just write R = L then R is just a new reference on the same list L.

if you also need to copy the elements in your list, use copy.deepcopy; R = L[:] only produces a shallow copy (which is fine in your case where there are only ints in the list).

How to reverse a generic list without changing the same list?

The "easy" option would be to just iterate the list in reverse order without actually changing the list itself instead of trying to reverse it the first time and know to do nothing the other times:

foreach (string t1 in t.List.AsEnumerable().Reverse())
{
//Some code
}

By using the LINQ Reverse method instead of the List Reverse, we can iterate it backwards without mutating the list. The AsEnumerable needs to be there to prevent the List Reverse method from being used.

How to get a reversed list view on a list in Java?

Guava provides this: Lists.reverse(List)

List<String> letters = ImmutableList.of("a", "b", "c");
List<String> reverseView = Lists.reverse(letters);
System.out.println(reverseView); // [c, b, a]

Unlike Collections.reverse, this is purely a view... it doesn't alter the ordering of elements in the original list. Additionally, with an original list that is modifiable, changes to both the original list and the view are reflected in the other.



Related Topics



Leave a reply



Submit