Reverse the order of all nodes at even position in given Linked List

• Difficulty Level : Easy
• Last Updated : 23 Dec, 2021

Given a linked list A[] of N integers, the task is to reverse the order of all integers at an even position.

Examples:

Input: A[] = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
Output: 1 6 3 4 5 2
Explanation:  Nodes at even position in the given linked list are 2, 4 and 6. So, after reversing there order, the new linked list will be 1 -> 6 -> 3 -> 4 -> 5 -> 2 -> NULL.

Input:  A[] = 1 -> 5 -> 3->NULL
Output: 1 5 3

Approach: The given problem can be solved by maintaining two linked lists, one list for all odd positioned nodes and another list for all even positioned nodes. Traverse the given linked list which will be considered as the odd list. Hence, for all the nodes at even positions, remove it from the odd list and insert it to the front of the even node list. Since the nodes are added at the front, their order will be reversed. Merge the two lists at alternate positions which is the required answer.

Below is the implementation of the above approach.

Java

 // Java program for the above approach import java.util.*; class GFG{ // Node of the linked liststatic class Node {     int data;    Node next;};static Node start = null;// Function to reverse all the even// positioned node of given linked liststatic Node reverse_even(Node A){    // Stores the nodes with    // even positions    Node even = null;     // Stores the nodes with    // odd positions    Node odd = A;     // If size of list is less that    // 3, no change is required    if (odd==null || odd.next==null || odd.next.next==null)        return odd;     // Loop to traverse the list    while (odd!=null && odd.next!=null) {         // Store the even positioned        // node in temp        Node temp = odd.next;        odd.next = temp.next;         // Add the even node to the        // beginning of even list        temp.next = even;         // Make temp as new even list        even = temp;         // Move odd to it's next node        odd = odd.next;    }     odd = A;     // Merge the evenlist into    // odd list alternatively    while (even!=null) {         // Stores the even's next        // node in temp        Node temp = even.next;         // Link the next odd node        // to next of even node        even.next = odd.next;         // Link even to next odd node        odd.next = even;         // Make new even as temp node        even = temp;         // Move odd to it's 2nd next node        odd = odd.next.next;    }     return A;} // Function to add a node at// the beginning of Linked Liststatic void push(int new_data){    Node new_node = new Node();    new_node.data = new_data;    new_node.next = start;    start = new_node;} // Function to print nodes// in a given linked liststatic void printList(Node node){    while (node != null) {        System.out.print(node.data+ " ");        node = node.next;    }} // Driver Codepublic static void main(String[] args){       push(6);    push(5);    push(4);    push(3);    push(2);    push(1);     start = reverse_even(start);    printList(start); }} // This code is contributed by 29AjayKumar

Python3

 # Python program for the above approachstart = None # Node of the linked listclass Node:    def __init__(self, data):        self.data = data        self.next = None # Function to reverse all the even# positioned node of given linked listdef reverse_even(A):       # Stores the nodes with    # even positions    even = None     # Stores the nodes with    # odd positions    odd = A     # If size of list is less that    # 3, no change is required    if (odd == None or odd.next == None or odd.next.next == None):        return odd     # Loop to traverse the list    while (odd and odd.next):         # Store the even positioned        # node in temp        temp = odd.next        odd.next = temp.next         # Add the even node to the        # beginning of even list        temp.next = even         # Make temp as new even list        even = temp         # Move odd to it's next node        odd = odd.next     odd = A     # Merge the evenlist into    # odd list alternatively    while (even):         # Stores the even's next        # node in temp        temp = even.next         # Link the next odd node        # to next of even node        even.next = odd.next         # Link even to next odd node        odd.next = even         # Make new even as temp node        even = temp         # Move odd to it's 2nd next node        odd = odd.next.next    return A # Function to add a node at# the beginning of Linked Listdef push(new_data):    global start    new_node = Node(new_data)    new_node.next = start    start = new_node # Function to print nodes# in a given linked listdef printList(node):    while (node != None):        print(node.data, end=" ")        node = node.next # Driver Codestart = None push(6)push(5)push(4)push(3)push(2)push(1) start = reverse_even(start)printList(start) # This code is contributed by saurabh_jaiswal.

C#

 // C# program for the above approachusing System;public class GFG{ // Node of the linked listclass Node {     public int data;    public Node next;};static Node start = null;   // Function to reverse all the even// positioned node of given linked liststatic Node reverse_even(Node A){       // Stores the nodes with    // even positions    Node even = null;     // Stores the nodes with    // odd positions    Node odd = A;     // If size of list is less that    // 3, no change is required    if (odd==null || odd.next==null || odd.next.next==null)        return odd;     // Loop to traverse the list    while (odd!=null && odd.next!=null) {         // Store the even positioned        // node in temp        Node temp = odd.next;        odd.next = temp.next;         // Add the even node to the        // beginning of even list        temp.next = even;         // Make temp as new even list        even = temp;         // Move odd to it's next node        odd = odd.next;    }     odd = A;     // Merge the evenlist into    // odd list alternatively    while (even!=null) {         // Stores the even's next        // node in temp        Node temp = even.next;         // Link the next odd node        // to next of even node        even.next = odd.next;         // Link even to next odd node        odd.next = even;         // Make new even as temp node        even = temp;         // Move odd to it's 2nd next node        odd = odd.next.next;    }     return A;} // Function to add a node at// the beginning of Linked Liststatic void push(int new_data){    Node new_node = new Node();    new_node.data = new_data;    new_node.next = start;    start = new_node;} // Function to print nodes// in a given linked liststatic void printList(Node node){    while (node != null) {        Console.Write(node.data+ " ");        node = node.next;    }} // Driver Codepublic static void Main(String[] args){    push(6);    push(5);    push(4);    push(3);    push(2);    push(1);     start = reverse_even(start);    printList(start); }} // This code is contributed by shikhasingrajput

Javascript


Output
1 6 3 4 5 2

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up