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

Examples:

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

Input:   10->22->30->43->56->70
Output:  10->30->56->22->43->70

The important thing in this question is to make sure that all below cases are handled
1) Empty linked list
2) A linked list with only one node
3) A linked list with only two nodes
4) A linked list with odd number of nodes
5) A linked list with even number of nodes

The below program maintains two pointers ‘odd’ and ‘even’ for current nodes at odd an even positions respectively. We also store first node of even linked list so that we can attach the even list at the end of odd list after all odd and even nodes are connected together in two different lists.

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20rearrange%20a%20linked%20list%20in%20such%20a%0A%2F%2F%20way%20that%20all%20odd%20positioned%20node%20are%20stored%20before%0A%2F%2F%20all%20even%20positioned%20nodes%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Linked%20List%20Node%0Astruct%20Node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20Node*%20next%3B%0A%7D%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20create%20a%20new%20node%0ANode*%20newNode(int%20key)%0A%7B%0A%20%20%20%20Node%20*temp%20%3D%20new%20Node%3B%0A%20%20%20%20temp-%3Edata%20%3D%20key%3B%0A%20%20%20%20temp-%3Enext%20%3D%20NULL%3B%0A%20%20%20%20return%20temp%3B%0A%7D%0A%20%0A%2F%2F%20Rearranges%20given%20linked%20list%20such%20that%20all%20even%0A%2F%2F%20positioned%20nodes%20are%20before%20odd%20positioned.%0A%2F%2F%20Returns%20new%20head%20of%20linked%20List.%0ANode%20*rearrangeEvenOdd(Node%20*head)%0A%7B%0A%20%20%20%20%2F%2F%20Corner%20case%0A%20%20%20%20if%20(head%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%20NULL%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20first%20nodes%20of%20even%20and%0A%20%20%20%20%2F%2F%20odd%20lists%0A%20%20%20%20Node%20*odd%20%3D%20head%3B%0A%20%20%20%20Node%20*even%20%3D%20head-%3Enext%3B%0A%20%0A%20%20%20%20%2F%2F%20Remember%20the%20first%20node%20of%20even%20list%20so%0A%20%20%20%20%2F%2F%20that%20we%20can%20connect%20the%20even%20list%20at%20the%0A%20%20%20%20%2F%2F%20end%20of%20odd%20list.%0A%20%20%20%20Node%20*evenFirst%20%3D%20even%3B%0A%20%0A%20%20%20%20while%20(1)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20there%20are%20no%20more%20nodes%2C%20then%20connect%0A%20%20%20%20%20%20%20%20%2F%2F%20first%20node%20of%20even%20list%20to%20the%20last%20node%0A%20%20%20%20%20%20%20%20%2F%2F%20of%20odd%20list%0A%20%20%20%20%20%20%20%20if%20(!odd%20%7C%7C%20!even%20%7C%7C%20!(even-%3Enext))%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd-%3Enext%20%3D%20evenFirst%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Connecting%20odd%20nodes%0A%20%20%20%20%20%20%20%20odd-%3Enext%20%3D%20even-%3Enext%3B%0A%20%20%20%20%20%20%20%20odd%20%3D%20even-%3Enext%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20there%20are%20NO%20more%20even%20nodes%20after%0A%20%20%20%20%20%20%20%20%2F%2F%20current%20odd.%0A%20%20%20%20%20%20%20%20if%20(odd-%3Enext%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20even-%3Enext%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd-%3Enext%20%3D%20evenFirst%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Connecting%20even%20nodes%0A%20%20%20%20%20%20%20%20even-%3Enext%20%3D%20odd-%3Enext%3B%0A%20%20%20%20%20%20%20%20even%20%3D%20odd-%3Enext%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20head%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20a%20linked%20list%0Avoid%20printlist(Node%20*%20node)%0A%7B%0A%20%20%20%20while%20(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20node-%3Edata%20%3C%3C%20%22-%3E%22%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%20%3C%3C%20%22NULL%22%20%3C%3C%20endl%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%0Aint%20main(void)%0A%7B%0A%20%20%20%20Node%20*head%20%3D%20newNode(1)%3B%0A%20%20%20%20head-%3Enext%20%3D%20newNode(2)%3B%0A%20%20%20%20head-%3Enext-%3Enext%20%3D%20newNode(3)%3B%0A%20%20%20%20head-%3Enext-%3Enext-%3Enext%20%3D%20newNode(4)%3B%0A%20%20%20%20head-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%20newNode(5)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Given%20Linked%20List%5Cn%22%3B%0A%20%20%20%20printlist(head)%3B%0A%20%0A%20%20%20%20head%20%3D%20rearrangeEvenOdd(head)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnModified%20Linked%20List%5Cn%22%3B%0A%20%20%20%20printlist(head)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output:

Given Linked List
1->2->3->4->5->NULL
Modified Linked List
1->3->5->2->4->NULL