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:
Extract alternate nodes starting from the second node (nodes at even positions).
Reverse the extracted list.
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
Output: 12->16->20->18->14
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
Output: 10->9->3->9->4->5->1->4
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.
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>usingnamespacestd;/* Structure of a linked list node */classNode{public:intdata;Node*next;// Constructor to initialize nodeNode(intval){data=val;next=NULL;}};/* Function to print the linked list */voidprintList(Node*head){// Traverse the list and print nodeswhile(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 */voidrearrange(Node*odd){// Base case: if list has less than 3 nodes, no change requiredif(odd==NULL||odd->next==NULL||odd->next->next==NULL)return;// Initialize even pointer to second nodeNode*even=odd->next;// Remove first even node and link first node to third nodeodd->next=odd->next->next;// Move odd pointer to next odd nodeodd=odd->next;// Initialize even list (start reversed list)even->next=NULL;// Traverse the listwhile(odd&&odd->next){// Store next odd nodeNode*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 nodeodd->next=temp;// Move odd pointer forward if next existsif(temp!=NULL)odd=temp;}// Append reversed even list at the end of odd listodd->next=even;}// Driver code intmain(){// Input: 10->4->9->1->3->5->9->4Node*head=newNode(10);head->next=newNode(4);head->next->next=newNode(9);head->next->next->next=newNode(1);head->next->next->next->next=newNode(3);head->next->next->next->next->next=newNode(5);head->next->next->next->next->next->next=newNode(9);head->next->next->next->next->next->next->next=newNode(4);rearrange(head);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>/* Structure of a linked list node */typedefstructNode{intdata;structNode*next;}Node;/* Function to print the linked list */voidprintList(Node*head){// Traverse the list and print nodeswhile(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 */voidrearrange(Node*odd){// Base case: if list has less than 3 nodes, no change requiredif(odd==NULL||odd->next==NULL||odd->next->next==NULL)return;// Initialize even pointer to second nodeNode*even=odd->next;// Remove first even node and link first node to third nodeodd->next=odd->next->next;// Move odd pointer to next odd nodeodd=odd->next;// Initialize even list (start reversed list)even->next=NULL;// Traverse the listwhile(odd&&odd->next){// Store next odd nodeNode*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 nodeodd->next=temp;// Move odd pointer forward if next existsif(temp!=NULL)odd=temp;}// Append reversed even list at the end of odd listodd->next=even;}// Driver code intmain(){// Input: 10->4->9->1->3->5->9->4Node*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);return0;}
Java
/* Structure of a linked list node */classNode{publicintdata;publicNodenext;// Constructor to initialize nodepublicNode(intval){data=val;next=null;}}/* Function to print the linked list */publicclassLinkedList{publicstaticvoidprintList(Nodehead){// Traverse the list and print nodeswhile(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 */publicstaticvoidrearrange(Nodeodd){// Base case: if list has less than 3 nodes, no change requiredif(odd==null||odd.next==null||odd.next.next==null)return;// Initialize even pointer to second nodeNodeeven=odd.next;// Remove first even node and link first node to third nodeodd.next=odd.next.next;// Move odd pointer to next odd nodeodd=odd.next;// Initialize even list (start reversed list)even.next=null;// Traverse the listwhile(odd!=null&&odd.next!=null){// Store next odd nodeNodetemp=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 nodeodd.next=temp;// Move odd pointer forward if next existsif(temp!=null)odd=temp;}// Append reversed even list at the end of odd listodd.next=even;}// Driver code publicstaticvoidmain(String[]args){// Input: 10->4->9->1->3->5->9->4Nodehead=newNode(10);head.next=newNode(4);head.next.next=newNode(9);head.next.next.next=newNode(1);head.next.next.next.next=newNode(3);head.next.next.next.next.next=newNode(5);head.next.next.next.next.next.next=newNode(9);head.next.next.next.next.next.next.next=newNode(4);rearrange(head);printList(head);}}
Python
# Structure of a linked list node classNode:def__init__(self,val):self.data=valself.next=None# Function to print the linked list defprintList(head):whilehead:print(head.data,end='')ifhead.next:print('->',end='')head=head.nextprint()# Function to rearrange the linked list such that# alternate nodes are reversed and appended at the end defrearrange(odd):# Base case: if list has less than 3 nodes, no change requiredifnotoddornotodd.nextornotodd.next.next:return# Initialize even pointer to second nodeeven=odd.next# Remove first even node and link first node to third nodeodd.next=odd.next.next# Move odd pointer to next odd nodeodd=odd.next# Initialize even list (start reversed list)even.next=None# Traverse the listwhileoddandodd.next:# Store next odd nodetemp=odd.next.next# Insert current even node at front of even list (reversing)odd.next.next=eveneven=odd.next# Link current odd node to next odd nodeodd.next=temp# Move odd pointer forward if next existsiftemp:odd=temp# Append reversed even list at the end of odd listodd.next=even# Driver code if__name__=="__main__":# Input: 10->4->9->1->3->5->9->4head=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 endusingSystem;publicclassLinkedList{Nodehead;publicclassNode{publicintdata;publicNodenext;publicNode(intitem){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 */voidrearrange(Nodeodd){// If linked list has less than 3// nodes, no change is requiredif(odd==null||odd.next==null||odd.next.next==null){return;}// even points to the beginning of even listNodeeven=odd.next;// Remove the first even nodeodd.next=odd.next.next;// odd points to next node in odd listodd=odd.next;// Set terminator for even listeven.next=null;// Traverse the listwhile(odd.next!=null){// Store the next node in odd listNodetemp=odd.next.next;// Link the next even node at// the beginning of even listodd.next.next=even;even=odd.next;// Remove the even node from middleodd.next=temp;// Move odd to the next odd nodeif(temp!=null){odd=temp;}}// Append the even list at the end of odd listodd.next=even;}/* Function to print nodes in a given linked list */voidprintList(Nodenode){while(node!=null){Console.Write(node.data+" ");node=node.next;}}// Driver codepublicstaticvoidMain(){LinkedListlist=newLinkedList();list.head=newNode(1);list.head.next=newNode(2);list.head.next.next=newNode(3);list.head.next.next.next=newNode(4);list.head.next.next.next.next=newNode(5);list.head.next.next.next.next.next=newNode(6);list.head.next.next.next.next.next.next=newNode(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 */classNode{constructor(val){this.data=val;this.next=null;}}/* Function to print the linked list */functionprintList(head){letcurrent=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 */functionrearrange(odd){// Base case: if list has less than 3 nodes, no change requiredif(!odd||!odd.next||!odd.next.next)return;// Initialize even pointer to second nodeleteven=odd.next;// Remove first even node and link first node to third nodeodd.next=odd.next.next;// Move odd pointer to next odd nodeodd=odd.next;// Initialize even list (start reversed list)even.next=null;// Traverse the listwhile(odd&&odd.next){// Store next odd nodelettemp=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 nodeodd.next=temp;// Move odd pointer forward if next existsif(temp)odd=temp;}// Append reversed even list at the end of odd listodd.next=even;}// Driver code lethead=newNode(10);head.next=newNode(4);head.next.next=newNode(9);head.next.next.next=newNode(1);head.next.next.next.next=newNode(3);head.next.next.next.next.next=newNode(5);head.next.next.next.next.next.next=newNode(9);head.next.next.next.next.next.next.next=newNode(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.