Rearrange a linked list
Submissions: 8566   Accuracy:

42.81%

  Difficulty: Easy   Marks: 2

Given a singly linked list, the task is to rearrange it in a way that all odd position nodes are together and all even positions node are together.

Input: 
First line of each testcase consists of number of testcases T. For each testcase, there will be two lines, first of which contains the length of the linked list and next will contains the elements of the linked list.

Output: 
For each testcase, output the linked list after rearranging the nodes in it.

User Task:
The task is to complete the function rearrangeEvenOdd() which rearranges the nodes in the linked list as required.

Constraints:
1 <= T <= 50
1 <= N <= 100
1 <= value <= 1000

Example:
Input:

2
4
1 2 3 4
5
1 2 3 4 5

Output:
1 3 2 4
1 3 5 2 4

Explanation:
Testcase 1:
 After rearranging the linked list elements in odd even fashion 1, 3 are odd and they are together. Also, 2 and 4 are even and they are together.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: kartik


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.