Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5604 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3413 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 799 |

@sriramgoparaju99 | 737 |

@Manikanta punnam | 629 |

@rdakka | 606 |

@prankursharma31 | 597 |

@praveenbgp6 | 546 |

@sanskar94511 | 531 |

@purohitmn02 | 529 |

@yashkaril4 | 517 |

@abhikarshgupta | 474 |

@rohitgarg2825 | 464 |

Complete Leaderboard |

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.

Assume the first element to be at position 1 followed by second element at position 2 and so on.

**Example 1:**

**Input:
**LinkedList: 1->2->3->4
**Output: **1 3 2 4
**Explanation: **
Odd elements are 1, 3 and even elements are
2, 4. Hence, resultant linked list is
1->3->2->4.

**Example 2:**

**Input:
**LinkedList: 1->2->3->4->5
**Output: **1 3 5 2 4
**Explanation:**
Odd elements are 1, 3, 5 and even elements are
2, 4. Hence, resultant linked list is
â€‹1->3->5->2->4.

**Your Task:**

The task is to complete the function **rearrangeEvenOdd**() which rearranges the nodes in the linked list as required and return the head of the linked list.

**Expected Time Complexity: **O(N).

**Expected Auxiliary Space: **O(1).

**Constraints:**

1 <= Size of the linked list <= 10000

1 <= values of linked list<= 1000

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Rearrange a linked list

...