C++ Program for Arranging Single Linked List in Alternate Odd and Even Nodes Order

Introduction:

Linked lists are fundamental data structures in computer science, providing an efficient way to organize and manipulate data. One interesting problem in the realm of linked lists is the arrangement of nodes in alternate odd and even order. This task involves reordering the nodes in such a way that all odd-positioned nodes appear first, followed by the even-positioned nodes.

Understanding the Problem

Consider a linked list with nodes numbered as 1, 2, 3, 4, and so on. Our goal is to rearrange the nodes so that all nodes at odd positions come first, followed by nodes at even positions. For example, given the linked list:

The desired arrangement is:

Algorithm Overview:

The algorithm involves iterating through the linked list, identifying odd and even nodes, and rearranging them to create a new list with alternating odd and even nodes. Let's break down the steps:

  • Create a linked list with random values for demonstration purposes.
  • Traverse the original linked list, separating nodes into odd and even groups.
  • Create two separate linked lists for odd and even nodes.
  • Concatenate the odd and even lists alternately to form the final arranged list.
  • Display the original and arranged lists for comparison.

Approach 1: Separate Odd and Even Nodes, and then Merge

The first approach involves separating the odd and even nodes into two separate linked lists and then merging them alternately. Here's the C++ code for this approach:

Explanation:

  • This C++ program defines a singly linked list structure (Node) and provides a function (arrangeList) to rearrange the elements of the list such that all nodes with odd positions (1-based) are placed before nodes with even positions.
  • The arrangeList function takes a reference to the head of the linked list as a parameter and modifies the list in place.
  • The arrangeList function starts by checking if the list is either empty or has only one element, in which case no rearrangement is needed.
  • If there are more than one element in the list, the function initializes four pointers (oddHead, evenHead, oddTail, and evenTail) to manage two separate linked lists - one for nodes at odd positions and another for nodes at even positions.
  • The function then iterates through the original list, dividing nodes based on their position.
  • Nodes at odd positions are linked together to form a new list (oddHead to oddTail), and similarly, nodes at even positions form another list (evenHead to evenTail).
  • After iterating through the original list, the function sets the next pointers of the tail nodes of both odd and even lists to nullptr to terminate them.
  • Finally, the function merges the two lists by setting the next pointer of the tail node of the odd list to the head of the even list. The head of the modified list is updated to point to the head of the odd list.
  • The printList function is provided to print the elements of a linked list. In the main function, an example linked list is created, printed, then rearranged using the arrangeList function, and the modified list is printed again to demonstrate the functionality of the program.

Program Output:

C++ Program for Arranging Single Linked List in Alternate Odd and Even Nodes Order

Approach 2: In-Place Rearrangement

This approach involves rearranging the nodes in place without using additional data structures. Here's the C++ code for this approach:

Explanation:

  • This C++ program defines a singly linked list structure (Node) and provides a function (arrangeList) to rearrange the elements of the list such that all nodes with even positions (0-based) come after nodes with odd positions.
  • The arrangeList function takes a reference to the head of the linked list as a parameter and modifies the list in place.
  • The arrangeList function uses three pointers: odd, even, and It initializes odd to the head of the list and even to the second node (if it exists).
  • The evenStart pointer is used to keep track of the starting point of the even-positioned nodes, allowing easy linking with the odd-positioned nodes later.
  • The function then iterates through the list, updating pointers to rearrange nodes. In each iteration, it moves the odd pointer to the next odd-positioned node and the even pointer to the next even-positioned node.
  • The odd->next is set to even->next to skip the even-positioned node in the current iteration, effectively linking odd-positioned nodes together. Simultaneously, even->next is updated to the next even-positioned node.
  • The loop continues until either even becomes nullptr or even->next becomes nullptr, indicating the end of the list. After the loop, the function links the last odd-positioned node to the starting point of the even-positioned nodes (evenStart).
  • The printList function is provided to print the elements of a linked list. In the main function, an example linked list is created, printed, then rearranged using the arrangeList function, and the modified list is printed again to demonstrate the functionality of the program.

Program Output:

C++ Program for Arranging Single Linked List in Alternate Odd and Even Nodes Order

Time Complexity:

The time complexity of the approach is O(N), where N is the number of nodes in the linked list. This is because the algorithm iterates through each node in the linked list once, and the operations performed within the loop are constant time.

Space Complexity:

The space complexity of the approach is O(1). It uses a constant amount of additional space for temporary pointers (oddHead, evenHead, oddTail, evenTail), regardless of the size of the linked list.

Conclusion:

In conclusion, the C++ program for arranging a single linked list in alternate odd and even nodes order provides a practical and efficient solution to reorganize the elements of a linked list.

By segregating odd and even nodes and then rearranging them in an alternating fashion, the program enhances the list's structure and readability. The algorithm employed demonstrates a systematic approach, iterating through the list and appropriately adjusting the pointers to achieve the desired order.

Moreover, the program showcases the versatility of C++ in implementing complex data structures and algorithms, leveraging the language's features like pointers and dynamic memory allocation. The modular design of the code promotes code reusability and maintainability, essential qualities in software development.

However, it's crucial to note that the program's effectiveness is contingent on the initial structure and content of the linked list. Careful consideration of edge cases and thorough testing is necessary to ensure the robustness and reliability of the program in diverse scenarios. Overall, the C++ program for arranging single linked lists in alternate odd and even nodes order offers a valuable tool for developers working with linked data structures, showcasing the power and flexibility of C++ in algorithmic implementations.






Latest Courses