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.
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Node of the linked listclass Node {public: int data; Node* next;};// Function to reverse all the even// positioned node of given linked listNode* 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 || !odd->next || !odd->next->next) return odd; // Loop to traverse the list while (odd && odd->next) { // 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) { // 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 Listvoid push(Node** head_ref, int new_data){ Node* new_node = new Node(); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node;}// Function to print nodes// in a given linked listvoid printList(Node* node){ while (node != NULL) { cout << node->data << " "; node = node->next; }}// Driver Codeint main(){ Node* start = NULL; push(&start, 6); push(&start, 5); push(&start, 4); push(&start, 3); push(&start, 2); push(&start, 1); start = reverse_even(start); printList(start); return 0;} |
Java
// Java program for the above approachimport 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 = Nonepush(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
<script>// Javascript program for the above approach// Node of the linked listclass Node { constructor(data) { this.data = data; this.next = null; }};// Function to reverse all the even// positioned node of given linked listfunction reverse_even(A) { // Stores the nodes with // even positions let even = null; // Stores the nodes with // odd positions let 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 && odd.next) { // Store the even positioned // node in temp let 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 let 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 Listfunction push(new_data) { let new_node = new Node(); new_node.data = new_data; new_node.next = start; start = new_node;}// Function to print nodes// in a given linked listfunction printList(node) { while (node != null) { document.write(node.data + " "); node = node.next; }}// Driver Codelet start = null;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.</script> |
1 6 3 4 5 2
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
