answersLogoWhite

0

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

12y ago

What else can I help you with?

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?


How does the inplace quicksort algorithm efficiently sort elements in an array?

The inplace quicksort algorithm efficiently sorts elements in an array by recursively dividing the array into smaller subarrays based on a chosen pivot element. It then rearranges the elements so that all elements smaller than the pivot are on one side, and all elements larger are on the other. This process is repeated until the entire array is sorted. The algorithm's efficiency comes from its ability to sort elements in place without requiring additional memory allocation for new arrays.


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;


Is Dijkstra's algorithm a greedy algorithm?

Yes, Dijkstra's algorithm is a greedy algorithm because it makes decisions based on the current best option without considering future consequences.


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

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


What is the purpose and functionality of the randomized select algorithm in the context of sorting and selecting elements in a data structure?

The purpose of the randomized select algorithm is to efficiently find the kth smallest element in an unsorted list. It works by randomly selecting a pivot element, partitioning the list around that pivot, and recursively narrowing down the search space until the kth element is found. This algorithm is useful for selecting specific elements in a data structure without having to sort the entire list.


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.


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.