Given a linked list, reverse alternate nodes and append at the end

Last Updated : 24 Apr, 2026

Given a singly linked list, the task is to rearrange the list by performing the following operations:

  1. Extract alternate nodes starting from the second node (nodes at even positions).
  2. Reverse the extracted list.
  3. Append the reversed list at the end of the remaining original list.

Note: Perform all operations in-place without using any extra memory.

Examples: 

Input: 12->14->16->18->20
2056957850

Output: 12->16->20->18->14
2056957851

Explanation: Two lists are 12->16->20 and 14->18, reverse the 2nd list: 18->14. Merge the lists

Input: LinkedList: 10->4->9->1->3->5->9->4

2056957857

Output: 10->9->3->9->4->5->1->4

2056957859

Explanation: Alternative nodes in the given linked list are 4,1,5,4. Reversing the alternative nodes from the given list, and then appending them to the end of the list results in a list 10->9->3->9->4->5->1->4.

Try It Yourself
redirect icon


Single Pass In-place Extraction and Reverse of Alternate Nodes – O(n) Time and O(1) Space

The idea is to rearrange the linked list by maintaining two separate lists: one for the odd positioned nodes (which forms the main list) and another for the even positioned nodes. While traversing the linked list, treat it initially as the odd list. Whenever an even positioned node is encountered, remove it from the odd list and insert it at the front of the even list. Adding nodes at the front ensures that the even list is formed in reversed order without requiring a separate reversal step. After processing all the nodes, append the even list at the end of the odd list to obtain the final rearranged linked list.

Algorithm:

  • If the linked list has fewer than 3 nodes, return as no change is needed.
  • Initialize two pointers: odd (head) and even (second node).
  • Remove the first even node and start the even list with it.
  • Traverse the list and for each even node: Remove it from the main list and Insert it at the front of the even list (to reverse it)
  • Continue linking only odd nodes together during traversal.
  • After traversal, append the reversed even list at the end of the odd list.

Illustration:


C++
#include <bits/stdc++.h>
using namespace std;

/* Structure of a linked list node */
class Node {
public:
    int data;
    Node* next;

    // Constructor to initialize node
    Node(int val) {
        data = val;
        next = NULL;
    }
};

/* Function to print the linked list */
void printList(Node* head) {

    // Traverse the list and print nodes
    while (head) {
        cout << head->data;
        if (head->next) cout << "->";
        head = head->next;
    }
    cout << endl;
}

/* Function to rearrange the linked list such that
   alternate nodes are reversed and appended at the end */
void rearrange(Node* odd) {

    // Base case: if list has less than 3 nodes, no change required
    if (odd == NULL || odd->next == NULL || odd->next->next == NULL)
        return;

    // Initialize even pointer to second node
    Node* even = odd->next;

    // Remove first even node and link first node to third node
    odd->next = odd->next->next;

    // Move odd pointer to next odd node
    odd = odd->next;

    // Initialize even list (start reversed list)
    even->next = NULL;

    // Traverse the list
    while (odd && odd->next) {

        // Store next odd node
        Node* temp = odd->next->next;

        // Insert current even node at front of even list (reversing)
        odd->next->next = even;
        even = odd->next;

        // Link current odd node to next odd node
        odd->next = temp;

        // Move odd pointer forward if next exists
        if (temp != NULL)
            odd = temp;
    }

    // Append reversed even list at the end of odd list
    odd->next = even;
}

// Driver code 
int main() {

    // Input: 10->4->9->1->3->5->9->4
    Node* head = new Node(10);
    head->next = new Node(4);
    head->next->next = new Node(9);
    head->next->next->next = new Node(1);
    head->next->next->next->next = new Node(3);
    head->next->next->next->next->next = new Node(5);
    head->next->next->next->next->next->next = new Node(9);
    head->next->next->next->next->next->next->next = new Node(4);


    rearrange(head);
    printList(head);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

/* Structure of a linked list node */
typedef struct Node {
    int data;
    struct Node* next;
} Node;

/* Function to print the linked list */
void printList(Node* head) {
    // Traverse the list and print nodes
    while (head) {
        printf("%d", head->data);
        if (head->next) printf("->");
        head = head->next;
    }
    printf("\n");
}

/* Function to rearrange the linked list such that
   alternate nodes are reversed and appended at the end */
void rearrange(Node* odd) {
    // Base case: if list has less than 3 nodes, no change required
    if (odd == NULL || odd->next == NULL || odd->next->next == NULL)
        return;

    // Initialize even pointer to second node
    Node* even = odd->next;

    // Remove first even node and link first node to third node
    odd->next = odd->next->next;

    // Move odd pointer to next odd node
    odd = odd->next;

    // Initialize even list (start reversed list)
    even->next = NULL;

    // Traverse the list
    while (odd && odd->next) {
        // Store next odd node
        Node* temp = odd->next->next;

        // Insert current even node at front of even list (reversing)
        odd->next->next = even;
        even = odd->next;

        // Link current odd node to next odd node
        odd->next = temp;

        // Move odd pointer forward if next exists
        if (temp!= NULL)
            odd = temp;
    }

    // Append reversed even list at the end of odd list
    odd->next = even;
}

// Driver code 
int main() {
    // Input: 10->4->9->1->3->5->9->4
    Node* head = (Node*)malloc(sizeof(Node));
    head->data = 10;
    head->next = (Node*)malloc(sizeof(Node));
    head->next->data = 4;
    head->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->data = 9;
    head->next->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->next->data = 1;
    head->next->next->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->next->next->data = 3;
    head->next->next->next->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->next->next->next->data = 5;
    head->next->next->next->next->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->next->next->next->next->data = 9;
    head->next->next->next->next->next->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->next->next->next->next->next->data = 4;
    head->next->next->next->next->next->next->next->next = NULL;

    rearrange(head);
    printList(head);

    return 0;
}
Java
/* Structure of a linked list node */
class Node {
    public int data;
    public Node next;

    // Constructor to initialize node
    public Node(int val) {
        data = val;
        next = null;
    }
}

/* Function to print the linked list */
public class LinkedList {
    public static void printList(Node head) {
        // Traverse the list and print nodes
        while (head!= null) {
            System.out.print(head.data);
            if (head.next!= null) System.out.print("->");
            head = head.next;
        }
        System.out.println();
    }

    /* Function to rearrange the linked list such that
       alternate nodes are reversed and appended at the end */
    public static void rearrange(Node odd) {
        // Base case: if list has less than 3 nodes, no change required
        if (odd == null || odd.next == null || odd.next.next == null)
            return;

        // Initialize even pointer to second node
        Node even = odd.next;

        // Remove first even node and link first node to third node
        odd.next = odd.next.next;

        // Move odd pointer to next odd node
        odd = odd.next;

        // Initialize even list (start reversed list)
        even.next = null;

        // Traverse the list
        while (odd!= null && odd.next!= null) {
            // Store next odd node
            Node temp = odd.next.next;

            // Insert current even node at front of even list (reversing)
            odd.next.next = even;
            even = odd.next;

            // Link current odd node to next odd node
            odd.next = temp;

            // Move odd pointer forward if next exists
            if (temp!= null)
                odd = temp;
        }

        // Append reversed even list at the end of odd list
        odd.next = even;
    }

    // Driver code 
    public static void main(String[] args) {
        // Input: 10->4->9->1->3->5->9->4
        Node head = new Node(10);
        head.next = new Node(4);
        head.next.next = new Node(9);
        head.next.next.next = new Node(1);
        head.next.next.next.next = new Node(3);
        head.next.next.next.next.next = new Node(5);
        head.next.next.next.next.next.next = new Node(9);
        head.next.next.next.next.next.next.next = new Node(4);

        rearrange(head);
        printList(head);
    }
}
Python
# Structure of a linked list node 
class Node:
    def __init__(self, val):
        self.data = val
        self.next = None

# Function to print the linked list 
def printList(head):
    while head:
        print(head.data, end='')
        if head.next:
            print('->', end='')
        head = head.next
    print()

# Function to rearrange the linked list such that
# alternate nodes are reversed and appended at the end 
def rearrange(odd):
    
    # Base case: if list has less than 3 nodes, no change required
    if not odd or not odd.next or not odd.next.next:
        return

    # Initialize even pointer to second node
    even = odd.next

    # Remove first even node and link first node to third node
    odd.next = odd.next.next

    # Move odd pointer to next odd node
    odd = odd.next

    # Initialize even list (start reversed list)
    even.next = None

    # Traverse the list
    while odd and odd.next:
     
        # Store next odd node
        temp = odd.next.next

        # Insert current even node at front of even list (reversing)
        odd.next.next = even
        even = odd.next

        # Link current odd node to next odd node
        odd.next = temp

        # Move odd pointer forward if next exists
        if temp:
            odd = temp

    # Append reversed even list at the end of odd list
    odd.next = even

# Driver code 
if __name__ == "__main__":
    # Input: 10->4->9->1->3->5->9->4
    head = Node(10)
    head.next = Node(4)
    head.next.next = Node(9)
    head.next.next.next = Node(1)
    head.next.next.next.next = Node(3)
    head.next.next.next.next.next = Node(5)
    head.next.next.next.next.next.next = Node(9)
    head.next.next.next.next.next.next.next = Node(4)

    rearrange(head)
    printList(head)
C#
// C# program to reverse alternate
// nodes of a linked list
// and append at the end
using System;

public class LinkedList {

    Node head;

    public class Node {

        public int data;
        public Node next;

        public Node(int item)
        {
            data = item;
            next = null;
        }
    }

    /* Function to reverse all even 
    positioned node and append at the end
    odd is the head node of given linked list */
    void rearrange(Node odd)
    {

        // If linked list has less than 3
        // nodes, no change is required
        if (odd == null || odd.next == null || odd.next.next == null) {
            return;
        }

        // even points to the beginning of even list
        Node even = odd.next;

        // Remove the first even node
        odd.next = odd.next.next;

        // odd points to next node in odd list
        odd = odd.next;

        // Set terminator for even list
        even.next = null;

        // Traverse the list
        while (odd.next != null) {

            // Store the next node in odd list
            Node temp = odd.next.next;

            // Link the next even node at
            // the beginning of even list
            odd.next.next = even;
            even = odd.next;

            // Remove the even node from middle
            odd.next = temp;

            // Move odd to the next odd node
            if (temp != null) {
                odd = temp;
            }
        }

        // Append the even list at the end of odd list
        odd.next = even;
    }

    /* Function to print nodes in a given linked list */
    void printList(Node node)
    {
        while (node != null) {
            Console.Write(node.data + " ");
            node = node.next;
        }
    }

    // Driver code
    public static void Main()
    {
        LinkedList list = new LinkedList();
        list.head = new Node(1);
        list.head.next = new Node(2);
        list.head.next.next = new Node(3);
        list.head.next.next.next = new Node(4);
        list.head.next.next.next.next = new Node(5);
        list.head.next.next.next.next.next = new Node(6);
        list.head.next.next.next.next.next.next = new Node(7);

        Console.WriteLine("Linked list before calling rearrange : ");
        list.printList(list.head);

        Console.WriteLine("");
        list.rearrange(list.head);

        Console.WriteLine("Linked list after calling rearrange : ");
        list.printList(list.head);
    }
}

/* This code contributed by PrinciRaj1992 */
JavaScript
/* Structure of a linked list node */
class Node {
    constructor(val) {
        this.data = val;
        this.next = null;
    }
}

/* Function to print the linked list */
function printList(head) {
    let current = head;
    while (current) {
        process.stdout.write(current.data.toString());
        if (current.next) process.stdout.write('->');
        current = current.next;
    }
    console.log();
}

/* Function to rearrange the linked list such that
   alternate nodes are reversed and appended at the end */
function rearrange(odd) {
    
    // Base case: if list has less than 3 nodes, no change required
    if (!odd || !odd.next || !odd.next.next) return;

    // Initialize even pointer to second node
    let even = odd.next;

    // Remove first even node and link first node to third node
    odd.next = odd.next.next;

    // Move odd pointer to next odd node
    odd = odd.next;

    // Initialize even list (start reversed list)
    even.next = null;

    // Traverse the list
    while (odd && odd.next) {
        // Store next odd node
        let temp = odd.next.next;

        // Insert current even node at front of even list (reversing)
        odd.next.next = even;
        even = odd.next;

        // Link current odd node to next odd node
        odd.next = temp;

        // Move odd pointer forward if next exists
        if (temp) odd = temp;
    }

    // Append reversed even list at the end of odd list
    odd.next = even;
}

// Driver code 
let head = new Node(10);
head.next = new Node(4);
head.next.next = new Node(9);
head.next.next.next = new Node(1);
head.next.next.next.next = new Node(3);
head.next.next.next.next.next = new Node(5);
head.next.next.next.next.next.next = new Node(9);
head.next.next.next.next.next.next.next = new Node(4);

rearrange(head);
printList(head);

Output
Linked list before calling rearrange() 1 2 3 4 5 6 7 
Linked list after calling rearrange() 1 3 5 7 6 4 2 

Time Complexity: O(n), The above code simply traverses the given linked list. So time complexity is O(n)
Auxiliary Space: O(1), No extra space is required.

Comment