203k views
3 votes
Write an algorithm for printing a singly linked list in reverse, using only constant extra space. This instruction implies that you cannot use recursion, but you may assume that your algorithm is a list member function.

User Heloise
by
6.2k points

1 Answer

5 votes

Answer:

- Loop through the list

- pop each item in the list

- unshift the popped items to the same list

- print the new list.

Step-by-step explanation:

The algorithm above can be replaced with the reverse method in JavaScript and Python like,

My_list.reverse()

User Gianluca
by
5.9k points