Segregate Odd-Even
Posted: 5 Dec, 2020
Difficulty: Moderate
There is a wedding ceremony at NinjaLand. The bride and groom want everybody to play a game and thus, they have blindfolded the attendees. The people from the bride’s side are holding odd numbers and people from the groom’s side are holding the even numbers. For the game to start quickly, all the bride’s side people should come first, followed by the groom’s side people in the same order.
The attendees of the wedding with their numbers are given in the form of a Singly Linked List, arranged randomly.
A singly linked list is a type of linked list that is unidirectional; that is, it can be traversed in only one direction from head to the last node (tail).
Example:
The attendees holding numbers from 1, 4, 3 are shown:
As the organizers are busy, you have to arrange all the people by arranging the bride’s side people first followed by the groom’s side people sequentially.
Note:
For the above example 1 -> 4 -> 3, 1 -> 3 -> 4 is the only correct answer, i.e nodes should be grouped sequentially. Hence, 3 -> 1 -> 4 is the wrong answer as we have to preserve the same order.
Input Format
The first line of input contains an integer T, the number of test cases. Then each test case follows.
The first and the only line of every test case contains the elements of the singly linked list separated by a single space and terminated by -1.
Hence, -1 would never be a list element.
Output Format:
For every test case, print the bride’s side attendees followed by the groom’s side attendees.
The attendees/elements should be single-space separated, terminated by -1.
The output of each test case is printed in a separate line.
Note :
You don't need to print the output, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 5 * 10^4
0 <= data <= 10^4 and data != -1
Where 'N' denotes the number of elements in the Singly Linked List and 'data' represents the value of those elements.
Time Limit: 1 sec
Approach 1
As we have to arrange the nodes such that all the odd nodes should come before all the even nodes, we can move all the even valued nodes from their current positions to the end of the linked list.
Algorithm:
- Initialise a pointer ‘LAST_NODE’ by NULL.
- Traverse the linked list to the end and get the last node of the list which will be pointed by ‘LAST_NODE’.
- Initialize a pointer ‘NEW_END’ by ‘LAST_NODE’.
- Traverse the list and consider all the even valued nodes before the first odd valued node. Move them to the end of the list by connecting them after the ‘NEW_END’. Keep moving the ‘NEW_END’ forward.
- If the list is completely traversed, return the head of the linked list as the list only contains even valued nodes.
- Else, we encounter the first odd valued node, update the head pointer to point to this first odd node.
- Traverse the linked list from this node to the ‘LAST_NODE’, if any even valued node is encountered, move it to the end of the list by connecting them after the ‘NEW_END’. Keep moving the ‘NEW_END’ forward.
- After the above process, all the even nodes will come after all the odd nodes.
- Return the head of the modified linked list.
Approach 2
The idea is to split the linked list into two: one containing all odd valued nodes and others containing all the even valued nodes. And finally, attach the even node linked list after the odd node linked list.
Algorithm:
- Initialise four-pointers, ‘ODD_START, ‘ODD_END’, ‘EVEN_START’ and ‘EVEN_END’ by NULL.
‘ODD_START’ : Points to the starting node of the linked list having odd values.
‘ODD_END’ : Points to the ending node of the linked list having odd values.
‘EVEN_START’ : Points to the starting node of the linked list having even values.
‘EVEN_END’ : Points to the ending node of the linked list having even values. - Traverse the original linked list.
- If the current node’s value is odd:
- If ‘ODD_START’ is NULL, the current node will be the new ‘ODD_START’. Else, add the current node to the end of the odd list. Keep moving ‘ODD_END’.
- If the current node’s value is even:
- If ‘EVEN_START’ is NULL, the current node will be the new ‘EVEN_START’. Else, add the current node to the end of the even list i.e. the current node will become the node after ‘EVEN_END’. Keep moving ‘evenEnd’.
- If the current node’s value is odd:
- After the end of the loop, the linked list starting from ‘ODD_START’ will contain all the odd valued nodes and the linked list starting from ‘EVEN_START’ will contain all the even valued nodes.
- Add the even list after the odd list by pointing the next of the ‘ODD_END’ to the ‘EVEN_START’.
- Return the head of the modified linked list, i.e ‘ODD_START’.
SIMILAR PROBLEMS
Insertion In Circular Linked List
Posted: 21 Apr, 2022
Difficulty: Easy
Insertion In A Singly Linked List
Posted: 22 Apr, 2022
Difficulty: Easy
Remove Loop
Posted: 22 Apr, 2022
Difficulty: Moderate
Deletion In Doubly Linked List
Posted: 22 Apr, 2022
Difficulty: Easy
Insertion In Doubly Linked List
Posted: 24 Apr, 2022
Difficulty: Easy