answersLogoWhite

0


Best Answer

The actual code depends on whether the list is singly-linked or doubly-linked, however the algorithm is largely the same for both. Of course if the list is doubly-linked there is no need to reverse the list at all since the list can simply be traversed in reverse. However, for the sake of completeness, example code is provided for both.

The Algorithm

Set the current node to be the head node then repeatedly extract the current node's next node and insert it at the head of the list until the current node's next node is NULL.



Singly-linked Example (C++)


Assuming the list is a reference that has a head node pointer, and each node in the list has a next node pointer, a singly-linked list can be reversed as follows:

// Ensure there is a head node.
if(Node* current = list.head)

{

// Ensure the current node has a next node.

while(Node* temp= current->next )

{

// Move the next node to the head of the list.

current->next = temp->next;

temp->next = list.head;

list.head = temp;

}

}


Doubly-linked Example (C++)

In doubly-linked lists, it is assumed each node has a previous node pointer as well as a next node pointer. The algorithm is essentially the same but the node pointers obviously need to be adjusted in both directions, as follows:

// Ensure there is a head node.
if( Node* current = list.head )

{

// Ensure the current node has a next node.

while(Node* temp= current->next )

{

// Move the next node to the head of the list.

current->next = temp->next;

temp->next.prev = current;

temp->next = list.head;

list.head->prev = temp;

list.head = temp;

temp->prev = NULL;

}


// If the list also has a tail node, remember to reset it!

list.tail = current;

}

In both cases, when the while() loop finishes, the current node ends up pointing at the tail node. However, the current node never actually changes -- it always points to the same node, the original head node. As each loop progresses, the current node is demoted towards the tail, one position at a time, to eventually become the tail node. When the current node has no next node, the loop terminates and the list is completely reversed.


Repeating the reversal will naturally restore the list to its original order.


Circular Lists

The exact same algorithm can also be applied to circular lists. The only major difference is that you terminate the loop when temp points to the head of the list, rather than when it is NULL.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers

User Avatar

Wiki User

12y ago

Iterate through the list, retrieving elements.  As you retrieve them, place them on a second list, placing each at the head of list.  When you are done, the second list will be the reverse of the first list.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

You don't. Iterating backwards through a singly linked list is very inefficient. If you need this behavior, then you should change to using a doubly linked list or some other structure.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

create an empty list for output

while the input list is not empty do

move the first element of the input list to the beginning of the output list

end while;

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Traverse the list and reverse the direction of the *next pointers

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

algorithm for implementing singly linked list with c

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Whwt is the algorithm to reverse the elements of a single linked lists without using a temporary list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can I reverse a temporary custody agreement without going through the courts if it was voluntary?

How do I reverse a temporary custody statue 751 when both parties agree?


Why algorithm is use?

An algorithm is the soul of a computer program. A code without an algorithm is like a missile without a radar. Like a body without a soul cheers olga lednichenko


What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;


What is the big-O worst-case complexity of this algorithm?

Can't say without some detail about the algorithm in question.


What is use of sorting algorithm?

sorting means arranging a list of numbers or elements in an order (ascending or descending).


Write an algorithm in c to convert an infix expression to a postfix expressionexecute your algorithm with the following infix expression as your input. m nk pgh a bc?

An algorithm can not be written with the following infix expression without knowing what the expression is. Once this information is included a person will be able to know how to write the algorithm.


Can you drive your car without reverse?

No


Give 10 difference between dda and bresenham algorithm?

DDA algorithm involves floating-point operations, while Bresenham algorithm uses only integer operations. DDA algorithm calculates the exact position of each pixel, while Bresenham algorithm determines the closest pixel to the ideal line path. DDA algorithm can suffer from precision issues due to floating-point calculations, while Bresenham algorithm is more accurate and efficient. DDA algorithm is simpler to implement but slower than Bresenham algorithm. DDA algorithm is susceptible to rounding errors, while Bresenham algorithm is not. DDA algorithm can produce jagged lines due to rounding errors, while Bresenham algorithm generates smoother lines. DDA algorithm is suitable for both lines and circles, while Bresenham algorithm is primarily used for drawing lines. DDA algorithm can handle lines with any slope, while Bresenham algorithm is more efficient for lines with slopes close to 0 or 1. DDA algorithm involves multiplication and division operations, while Bresenham algorithm uses addition and subtraction operations. DDA algorithm is a general line drawing algorithm, while Bresenham algorithm is specialized for line drawing and rasterization.


How are alu elements unusual as retrotransposons?

Retrotransposons are subdivided into two categories: LTR and without LTR. Without LTR is also subdivided into two:LINE and SINE. We know that retrotransposons replicate themselves. However, Sıne have not got reverse transcriptase enzymes ,it doesn't repliate themselves without Line retrotranspozon.


Can you vacate a temporary custody order without an attorney?

It is possible to vacate a temporary custody order without an attorney by filing the paperwork with the court yourself. However, it is advisable to consult an attorney.


What are the conditions to swap in c plus plus without temporary?

You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;


What Feature to set temporary left and right margins?

You can use the CSS property padding to set temporary left and right margins on an element. You can set the padding on the left and right sides using padding-left and padding-right. This will create space around the content within the element without affecting the layout of surrounding elements.