Rearrange odd and even places

Moderate
0/80
1 upvote
Asked in company
Rupeek

Problem statement

Rearrange and return a linked list in such a way that all odd position nodes are together and all even positions node are together

You just need to return the head of new linked list, don't print the elements.

Detailed explanation ( Input/output format, Notes, Images )
Input format :
Line 1 : Linked list elements of length n (separated by space and terminated by -1)
Output format :
Updated list elements (separated by space)
Constraints :

1 <= n <= 10^4

Sample Input :
 1 2 3 4 5 6 7 8 -1

Note : -1 at the end of input is just a terminator representing the end of linked list. This -1 is not part of the linked list. Size of given linked list is 4.

Sample Output :
 1 3 5 7 2 4 6 8
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Rearrange odd and even places
Full screen
Console