Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The following steps can be followed to exchange the first and second halves of a doubly linked list:

  1. Traverse the doubly linked list to find its total length. Let the length be denoted by "n".

  2. Check if the length of the linked list is even or odd. If it is odd, we can consider the middle node as part of the first half.

  3. Traverse the linked list till the middle node and mark it as "mid".

  4. Traverse the linked list from mid to n and mark the last node as "last".

  5. Set the next pointer of "last" to the first node of the linked list.

  6. Set the previous pointer of the first node to "last".

  7. Set the next pointer of "mid" to NULL.

  8. Set the previous pointer of the node after "last" to NULL.

  9. Set the head pointer of the linked list to "last" and return it.

The above steps will successfully exchange the first and second halves of the doubly linked list.