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 the 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 reversed, attach it to the odd linked list.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
Node* rearrangeEvenOdd(Node* head)
{
if (head == NULL)
return NULL;
Node* odd = head;
Node* even = head->next;
Node* evenFirst = even;
while (1) {
if (!odd || !even || !(even->next)) {
break ;
}
odd->next = even->next;
odd = even->next;
if (odd->next == NULL) {
even->next = NULL;
break ;
}
even->next = odd->next;
even = odd->next;
}
Node* current = evenFirst;
Node* prev = NULL;
Node* front = NULL;
while (current != NULL) {
front = current->next;
current->next = prev;
prev = current;
current = front;
}
evenFirst = prev;
odd->next = evenFirst;
return head;
}
void printlist(Node* node)
{
while (node != NULL) {
cout << node->data << " -> " ;
node = node->next;
}
cout << "NULL" << endl;
}
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;
}
|
Java
class sol
{
static class Node
{
int data;
Node next;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
static Node rearrangeEvenOdd(Node head)
{
if (head == null )
return null ;
Node odd = head;
Node even = head.next;
Node evenFirst = even;
while ( true )
{
if (odd == null || even == null || (even.next) == null )
{
break ;
}
odd.next = even.next;
odd = even.next;
if (odd.next == null )
{
even.next = null ;
break ;
}
even.next = odd.next;
even = odd.next;
}
Node current = evenFirst;
Node prev = null ;
Node front = null ;
while (current != null )
{
front = current.next;
current.next = prev;
prev = current;
current = front;
}
evenFirst = prev;
odd.next = evenFirst;
return head;
}
static void printlist(Node node)
{
while (node != null )
{
System.out.print( node.data + " -> " );
node = node.next;
}
System.out.println( "null" );
}
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);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode(key):
temp = Node(key)
temp.data = key
temp. next = None
return temp
def rearrangeEvenOdd(head):
if (head = = None ):
return None
odd = head
even = head. next
evenFirst = even
while True :
if (odd = = None or even = = None or
(even. next ) = = None ):
break
odd. next = even. next
odd = even. next
if (odd. next = = None ):
even. next = None
break
even. next = odd. next
even = odd. next
current = evenFirst
prev = None
front = None
while (current ! = None ):
front = current. next
current. next = prev
prev = current
current = front
evenFirst = prev
odd. next = evenFirst
return head
def printlist(node):
while (node ! = None ) :
print (node.data, end = "->" )
node = node. next
print ( "NULL" )
if __name__ = = '__main__' :
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)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
static Node rearrangeEvenOdd(Node head)
{
if (head == null )
return null ;
Node odd = head;
Node even = head.next;
Node evenFirst = even;
while ( true )
{
if (odd == null || even == null ||
(even.next) == null )
{
break ;
}
odd.next = even.next;
odd = even.next;
if (odd.next == null )
{
even.next = null ;
break ;
}
even.next = odd.next;
even = odd.next;
}
Node current = evenFirst;
Node prev = null ;
Node front = null ;
while (current != null )
{
front = current.next;
current.next = prev;
prev = current;
current = front;
}
evenFirst = prev;
odd.next = evenFirst;
return head;
}
static void printlist(Node node)
{
while (node != null )
{
Console.Write( node.data + " -> " );
node = node.next;
}
Console.WriteLine( "null" );
}
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);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function newNode(key) {
var temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
function rearrangeEvenOdd(head) {
if (head == null )
return null ;
var odd = head;
var even = head.next;
var evenFirst = even;
while ( true ) {
if (odd == null || even == null ||
(even.next) == null )
{
break ;
}
odd.next = even.next;
odd = even.next;
if (odd.next == null ) {
even.next = null ;
break ;
}
even.next = odd.next;
even = odd.next;
}
var current = evenFirst;
var prev = null ;
var front = null ;
while (current != null ) {
front = current.next;
current.next = prev;
prev = current;
current = front;
}
evenFirst = prev;
odd.next = evenFirst;
return head;
}
function printlist(node) {
while (node != null ) {
document.write(node.data + " -> " );
node = node.next;
}
document.write( "Null" );
}
var 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);
</script>
|
Output
1 -> 3 -> 5 -> 6 -> 4 -> 2 -> NULL
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of nodes in the linked list.
- Auxiliary Space: O(1), as we are not using any extra space for the string.
Last Updated :
13 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...