Java Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order
Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.
There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where the first node is even, second odd, third even and so on.
Input: 11 -> 20 -> 40 -> 55 -> 77 -> 80 -> NULL Output: 11 -> 20 -> 55 -> 40 -> 77 -> 80 -> NULL 20, 40, 80 occur in even positions and 11, 55, 77 occur in odd positions. Input: 10 -> 1 -> 2 -> 3 -> 5 -> 6 -> 7 -> 8 -> NULL Output: 1 -> 10 -> 3 -> 2 -> 5 -> 6 -> 7 -> 8 -> NULL 1, 3, 5, 7 occur in odd positions and 10, 2, 6, 8 occur at even positions in the list
Method 1 (Simple):
In this method, we create two stacks-Odd and Even. We traverse the list and when we encounter an even node in an odd position we push this node’s address onto Even Stack. If we encounter an odd node in an even position we push this node’s address onto Odd Stack.
After traversing the list, we simply pop the nodes at the top of the two stacks and exchange their data. We keep repeating this step until the stacks become empty.
Step 1: Create two stacks Odd and Even. These stacks will store the pointers to the nodes in the list Step 2: Traverse list from start to end, using the variable current. Repeat following steps 3-4. Step 3: If the current node is even and it occurs at an odd position, push this node's address to stack Even. Step 4: If the current node is odd and it occurs at an even position, push this node's address to stack Odd.
[END OF TRAVERSAL]. Step 5: The size of both the stacks will be the same. While both the stacks are not empty exchange the nodes at the top of the two stacks and pop both nodes from their respective stacks. Step 6: The List is now rearranged. STOP
Linked List: 1 2 3 5 6 7 8 Linked List after Rearranging: 1 2 3 6 5 8 7
Time Complexity : O(n)
Auxiliary Space : O(n)
Method 2 (Efficient):
- Segregate odd and even values in the list. After this, all odd values will occur together followed by all even values.
- Split the list into two lists odd and even.
- Merge the even list into odd list
REARRANGE (HEAD) Step 1: Traverse the list using NODE TEMP. If TEMP is odd Add TEMP to the beginning of the List [END OF IF] [END OF TRAVERSAL] Step 2: Set TEMP to 2nd element of LIST. Step 3: Set PREV_TEMP to 1st element of List Step 4: Traverse using node TEMP as long as an even node is not encountered. PREV_TEMP = TEMP, TEMP = TEMP->NEXT [END OF TRAVERSAL] Step 5: Set EVEN to TEMP. Set PREV_TEMP->NEXT to NULL Step 6: I = HEAD, J = EVEN Step 7: Repeat while I != NULL and J != NULL Store next nodes of I and J in K and L K = I->NEXT, L = J->NEXT I->NEXT = J, J->NEXT = K, PTR = J I = K and J = L [END OF LOOP] Step 8: if I == NULL PTR->NEXT = J [END of IF] Step 8: Return HEAD. Step 9: End
Linked List: 10 2 1 5 3 6 7 8 Rearranged List 7 10 3 2 5 6 1 8
Time Complexity : O(n)
Auxiliary Space : O(1)
Please refer complete article on Alternate Odd and Even Nodes in a Singly Linked List for more details!