Append odd position nodes in reverse at the end of even positioned nodes in a Linked List

Given a linked list. The task is to segregate its even and odd position nodes in such a way that odd position nodes appear before even positioned nodes all the even positioned nodes must be in reverse order.

Examples:

Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
Output : 1 -> 3 -> 5 -> 6 -> 4 -> 2 -> NULL

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

Source: Microsoft Interview

Approach: A similar problem has been discussed in the given link, but there the even part was not reversed. Maintains two pointers odd and even for current nodes at odd and even positions respectively. Also store the first node of even linked list so that we can attach the even list at the end of the odd list after all odd and even nodes are connected together in two different lists. Once the even list has been separated, we just need to reverse it. Reversing a linked list can be found here. Once the even list is reverse, attach it to the odd linked list.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to Append odd position nodes
// in reverse at the end of even
// positioned nodes in a Linked List
#include <bits/stdc++.h>
using namespace std;
  
// Linked List Node
struct Node {
    int data;
    struct Node* next;
};
  
// A utility function to create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->next = NULL;
    return temp;
}
  
// Rearranges given linked list such that all even
// positioned nodes are before odd positioned 
// in a reverse
Node* rearrangeEvenOdd(Node* head)
{
    // Corner case
    if (head == NULL)
        return NULL;
  
    // Initialize first nodes of even and
    // odd lists
    Node* odd = head;
    Node* even = head->next;
  
    // Remember the first node of even list so
    // that we can connect the even list at the
    // end of odd list.
    Node* evenFirst = even;
  
    while (1) {
  
        // If there are no more nodes, then connect
        // first node of even list to the last node
        // of odd list
        if (!odd || !even || !(even->next)) {
            break;
        }
  
        // Connecting odd nodes
        odd->next = even->next;
        odd = even->next;
  
        // If there are NO more even nodes after
        // current odd.
        if (odd->next == NULL) {
            even->next = NULL;
            break;
        }
  
        // Connecting evenevenFirs nodes
        even->next = odd->next;
        even = odd->next;
    }
  
    // Reversal of even linked list
    Node* current = evenFirst;
    Node* prev = NULL;
    Node* front = NULL;
  
    // Iterate in the complete linked list
    while (current != NULL) {
        front = current->next;
        current->next = prev;
        prev = current;
        current = front;
    }
  
    evenFirst = prev;
  
    // Attach the reversed even linked
    // list to odd linked list
    odd->next = evenFirst;
    return head;
}
  
// A utility function to print a linked list
void printlist(Node* node)
{
    while (node != NULL) {
        cout << node->data << " -> ";
        node = node->next;
    }
    cout << "NULL" << endl;
}
  
// Driver code
int main(void)
{
    Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);
    head->next->next->next->next = newNode(5);
    head->next->next->next->next->next = newNode(6);
  
    head = rearrangeEvenOdd(head);
  
    printlist(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Append odd position nodes 
// in reverse at the end of even 
// positioned nodes in a Linked List 
class sol
{
  
// Linked List Node 
static class Node 
    int data; 
    Node next; 
}; 
  
// A utility function to create a new node 
static Node newNode(int key) 
    Node temp = new Node(); 
    temp.data = key; 
    temp.next = null
    return temp; 
  
// Rearranges given linked list such that all even 
// positioned nodes are before odd positioned 
// in a reverse 
static Node rearrangeEvenOdd(Node head) 
    // Corner case 
    if (head == null
        return null
  
    // Initialize first nodes of even and 
    // odd lists 
    Node odd = head; 
    Node even = head.next; 
  
    // Remember the first node of even list so 
    // that we can connect the even list at the 
    // end of odd list. 
    Node evenFirst = even; 
  
    while (true
    
  
        // If there are no more nodes, then connect 
        // first node of even list to the last node 
        // of odd list 
        if (odd == null || even == null || (even.next) == null
        
            break
        
  
        // Connecting odd nodes 
        odd.next = even.next; 
        odd = even.next; 
  
        // If there are NO more even nodes after 
        // current odd. 
        if (odd.next == null
        
            even.next = null
            break
        
  
        // Connecting evenevenFirs nodes 
        even.next = odd.next; 
        even = odd.next; 
    
  
    // Reversal of even linked list 
    Node current = evenFirst; 
    Node prev = null
    Node front = null
  
    // Iterate in the complete linked list 
    while (current != null)
    
        front = current.next; 
        current.next = prev; 
        prev = current; 
        current = front; 
    
  
    evenFirst = prev; 
  
    // Attach the reversed even linked 
    // list to odd linked list 
    odd.next = evenFirst; 
    return head; 
  
// A utility function to print a linked list 
static void printlist(Node node) 
    while (node != null
    
        System.out.print( node.data + " -> "); 
        node = node.next; 
    
    System.out.println( "null" ); 
  
// Driver code 
public static void main(String args[])
    Node head = newNode(1); 
    head.next = newNode(2); 
    head.next.next = newNode(3); 
    head.next.next.next = newNode(4); 
    head.next.next.next.next = newNode(5); 
    head.next.next.next.next.next = newNode(6); 
  
    head = rearrangeEvenOdd(head); 
  
    printlist(head); 
  
}
  
// This code is contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Append odd position nodes 
// in reverse at the end of even 
// positioned nodes in a Linked List 
using System;
      
class GFG
{
  
// Linked List Node 
public class Node 
    public int data; 
    public Node next; 
}; 
  
// A utility function to create a new node 
static Node newNode(int key) 
    Node temp = new Node(); 
    temp.data = key; 
    temp.next = null
    return temp; 
  
// Rearranges given linked list such that 
// all even positioned nodes are before 
// odd positioned in a reverse 
static Node rearrangeEvenOdd(Node head) 
    // Corner case 
    if (head == null
        return null
  
    // Initialize first nodes of even and 
    // odd lists 
    Node odd = head; 
    Node even = head.next; 
  
    // Remember the first node of even list so 
    // that we can connect the even list at the 
    // end of odd list. 
    Node evenFirst = even; 
  
    while (true
    
  
        // If there are no more nodes,  
        // then connect first node of
        // even list to the last node 
        // of odd list 
        if (odd == null || even == null ||
                          (even.next) == null
        
            break
        
  
        // Connecting odd nodes 
        odd.next = even.next; 
        odd = even.next; 
  
        // If there are NO more even nodes after 
        // current odd. 
        if (odd.next == null
        
            even.next = null
            break
        
  
        // Connecting evenevenFirs nodes 
        even.next = odd.next; 
        even = odd.next; 
    
  
    // Reversal of even linked list 
    Node current = evenFirst; 
    Node prev = null
    Node front = null
  
    // Iterate in the complete linked list 
    while (current != null)
    
        front = current.next; 
        current.next = prev; 
        prev = current; 
        current = front; 
    
  
    evenFirst = prev; 
  
    // Attach the reversed even linked 
    // list to odd linked list 
    odd.next = evenFirst; 
    return head; 
  
// A utility function to print a linked list 
static void printlist(Node node) 
    while (node != null
    
        Console.Write( node.data + " -> "); 
        node = node.next; 
    
    Console.WriteLine( "null" ); 
  
// Driver code 
public static void Main(String []args)
    Node head = newNode(1); 
    head.next = newNode(2); 
    head.next.next = newNode(3); 
    head.next.next.next = newNode(4); 
    head.next.next.next.next = newNode(5); 
    head.next.next.next.next.next = newNode(6); 
  
    head = rearrangeEvenOdd(head); 
  
    printlist(head); 
}
  
// This code is contributed by PrinciRaj1992 

chevron_right


Output:

1 -> 3 -> 5 -> 6 -> 4 -> 2 -> NULL


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : andrew1234, princiraj1992



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.