Rearrange a linked list in to alternate first and last element
Last Updated :
24 Feb, 2023
Given a linked list. arrange the linked list in manner of alternate first and last element.
Examples:
Input : 1->2->3->4->5->6->7->8
Output :1->8->2->7->3->6->4->5
Input :10->11->15->13
Output :10->13->11->15
We have discussed three different solution in Rearrange a given linked list in-place.
In this post a different Deque based solution is discussed.
Method:
- Create an empty deque
- Insert all element from the linked list to the deque
- Insert the element back to the linked list from deque in alternate fashion i.e first then last and so on
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void arrange( struct Node* head)
{
struct Node* temp = head;
deque< int > d;
while (temp != NULL) {
d.push_back(temp->data);
temp = temp->next;
}
int i = 0;
temp = head;
while (!d.empty()) {
if (i % 2 == 0) {
temp->data = d.front();
d.pop_front();
}
else {
temp->data = d.back();
d.pop_back();
}
i++;
temp = temp->next;
}
}
void push( struct Node** head_ref, char new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf ( "%d " , temp->data);
temp = temp->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
cout << "Given linked list\t" ;
printList(head);
arrange(head);
cout << "\nAfter rearrangement\t" ;
printList(head);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static class Node
{
int data;
Node next;
Node( int data)
{
this .data = data;
next = null ;
}
}
static void printList(Node head)
{
Node temp = head;
while (temp != null )
{
System.out.print(temp.data + " " );
temp = temp.next;
}
}
static void arrange(Node head)
{
Deque<Integer> deque = new ArrayDeque<>();
Node temp = head;
while (temp != null )
{
deque.addLast(temp.data);
temp = temp.next;
}
temp = head;
int i = 0 ;
while (!deque.isEmpty())
{
if (i % 2 == 0 )
{
temp.data = deque.removeFirst();
}
else
{
temp.data = deque.removeLast();
}
i++;
temp = temp.next;
}
}
public static void main (String[] args)
{
Node head = null ;
head = new Node( 1 );
head.next = new Node( 2 );
head.next.next = new Node( 3 );
head.next.next.next = new Node( 4 );
head.next.next.next.next = new Node( 5 );
System.out.println( "Given linked list" );
printList(head);
arrange(head);
System.out.println( "\nAfter rearrangement" );
printList(head);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def arrange( head):
temp = head
d = []
while (temp ! = None ) :
d.append(temp.data)
temp = temp. next
i = 0
temp = head
while ( len (d) > 0 ) :
if (i % 2 = = 0 ) :
temp.data = d[ 0 ]
d.pop( 0 )
else :
temp.data = d[ - 1 ]
d.pop()
i = i + 1
temp = temp. next
return head
def push( head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def printList( head):
temp = head
while (temp ! = None ) :
print ( temp.data,end = " " )
temp = temp. next
head = None
head = push(head, 5 )
head = push(head, 4 )
head = push(head, 3 )
head = push(head, 2 )
head = push(head, 1 )
print ( "Given linked list\t" )
printList(head)
head = arrange(head)
print ( "\nAfter rearrangement\t" )
printList(head)
|
C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
class Node {
public int data;
public Node next;
public Node( int val){
data = val;
next = null ;
}
}
class HelloWorld {
public static void arrange(Node head)
{
Node temp = head;
List< int > d = new List< int > ();
while (temp != null ) {
d.Add(temp.data);
temp = temp.next;
}
int i = 0;
temp = head;
while (d.Count > 0) {
if (i % 2 == 0) {
temp.data = d.First();
d.RemoveAt(0);
}
else {
temp.data = d.Last();
d.RemoveAt(d.Count - 1);
}
i++;
temp = temp.next;
}
}
public static Node push(Node head, int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
return head;
}
public static void printList(Node head)
{
Node temp = head;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
}
static void Main() {
Node head = null ;
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
Console.Write( "Given linked list " );
printList(head);
arrange(head);
Console.Write( "\nAfter rearrangement " );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .next = null ;
}
};
function arrange( head)
{
var temp = head;
var d = [];
while (temp != null ) {
d.push(temp.data);
temp = temp.next;
}
var i = 0;
temp = head;
while (d.length!=0) {
if (i % 2 == 0) {
temp.data = d[0];
d.shift();
}
else {
temp.data = d[d.length-1];
d.pop();
}
i++;
temp = temp.next;
}
}
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function printList(head)
{
var temp = head;
while (temp != null ) {
document.write(temp.data+ " " );
temp = temp.next;
}
}
var head = null ;
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
document.write( "Given linked list " );
printList(head);
arrange(head);
document.write( "<br>After rearrangement " );
printList(head);
</script>
|
Output
Given linked list 1 2 3 4 5
After rearrangement 1 5 2 4 3
Time Complexity : O(nlogn)
Auxiliary space: O(n) because using deque
Share your thoughts in the comments
Please Login to comment...