Rearrange Odd and Even Position Nodes in Linked List

Rearrange a linked list such that all nodes at odd positions are together, followed by all nodes at even positions. You need to return the head of the reorganized linked list without displaying the elements.

Input:

Line 1: Linked list elements of length n (separated by space and terminated by -1)

Output:

Updated list elements (separated by space)

Example:

Input:
1 2 3 4 5 -1
Output:
1 3 5 2 4

Constraints:

  • 1 <= n < 10^4
AnswerBot
1y

The task is to rearrange a linked list such that all odd position nodes are together and all even position nodes are together.

  • Create two separate linked lists, one for odd position nodes and one for e...read more

Help your peers!
Add answer anonymously...
Rupeek Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter