Rearrange Linked List Odd Even

easy
1. You are given a singly linked list N nodes.
 2. You have to write a function that groups nodes in the list such that
    all odd nodes appear at the beginning of the list. After that all even
    nodes appear.
 3. Note: 
     1. Here we are talking about the node number and not the value in the nodes.
     2. The relative order inside both the even and odd groups should remain as
        it was in the input.
     3. The first node is considered odd, the second node even and so on.
     4. You should try to do it in place. The program should run in O(1)
        space complexity and O(N) time complexity.
 4. display is a utility function which displays the contents of Linked List,
    feel free to use it for debugging purposes.
 5. main takes input from the users and creates the Linked List. You can use
    display to know its contents.
 6. This is a functional problem. 
 7. You should code only the oddEvenList function. It takes as input the
    head of the linked list. It should return the head of the rearranged
    linked list.
 8. Don't change the code of Node, main and display.

Input Format

First line takes N, the number of elements in the list. Next line takes input N space separated numbers reperesenting elements of the linked list. Input is handled for you.

Output Format

Rearranged Linked List. Output is handled for you.

Constraints

1 <= N <= 1000

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
7
2 1 3 5 6 4 7
Output
2 3 6 7 1 5 4
Previous
Partition Linked List
Next
Remove Loop In Singly Linked List

Related Questions