Alternating split of a given Singly Linked List | Set 1
Last Updated :
14 Mar, 2023
Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller lists ‘a’ and ‘b’. The sublists should be made from alternating elements in the original list. So if the original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and the other should be 1->1->1.
Method 1(Simple)
The simplest approach iterates over the source list and pull nodes off the source and alternately put them at the front (or beginning) of ‘a’ and b’. The only strange part is that the nodes will be in the reverse order that occurred in the source list. Method 2 inserts the node at the end by keeping track of the last node in sublists.
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node* next;
};
void MoveNode(Node** destRef, Node** sourceRef) ;
void AlternatingSplit(Node* source, Node** aRef,
Node** bRef)
{
Node* a = NULL;
Node* b = NULL;
Node* current = source;
while (current != NULL)
{
MoveNode(&a, ¤t);
if (current != NULL)
{
MoveNode(&b, ¤t);
}
}
*aRef = a;
*bRef = b;
}
void MoveNode(Node** destRef, Node** sourceRef)
{
Node* newNode = *sourceRef;
assert (newNode != NULL);
*sourceRef = newNode->next;
newNode->next = *destRef;
*destRef = newNode;
}
void 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;
}
void printList(Node *node)
{
while (node!=NULL)
{
cout<<node->data<< " " ;
node = node->next;
}
}
int main()
{
Node* head = NULL;
Node* a = NULL;
Node* b = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
push(&head, 0);
cout<< "Original linked List: " ;
printList(head);
AlternatingSplit(head, &a, &b);
cout<< "\nResultant Linked List 'a' : " ;
printList(a);
cout<< "\nResultant Linked List 'b' : " ;
printList(b);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
struct Node
{
int data;
struct Node* next;
};
void MoveNode( struct Node** destRef, struct Node** sourceRef) ;
void AlternatingSplit( struct Node* source, struct Node** aRef,
struct Node** bRef)
{
struct Node* a = NULL;
struct Node* b = NULL;
struct Node* current = source;
while (current != NULL)
{
MoveNode(&a, ¤t);
if (current != NULL)
{
MoveNode(&b, ¤t);
}
}
*aRef = a;
*bRef = b;
}
void MoveNode( struct Node** destRef, struct Node** sourceRef)
{
struct Node* newNode = *sourceRef;
assert (newNode != NULL);
*sourceRef = newNode->next;
newNode->next = *destRef;
*destRef = newNode;
}
void push( struct node** head_ref, int 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 *node)
{
while (node!=NULL)
{
printf ( "%d " , node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL;
struct Node* a = NULL;
struct Node* b = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
push(&head, 0);
printf ( "\n Original linked List: " );
printList(head);
AlternatingSplit(head, &a, &b);
printf ( "\n Resultant Linked List 'a' " );
printList(a);
printf ( "\n Resultant Linked List 'b' " );
printList(b);
getchar ();
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node next;
Node( int data) {
this .data = data;
next = null ;
}
}
class Main
{
static void AlternatingSplit(Node source, Node[] aRef, Node[] bRef) {
Node a = null , b = null ;
Node current = source;
int count = 0 ;
while (current != null ) {
if (count % 2 == 0 ) {
if (a == null ) {
aRef[ 0 ] = current;
a = current;
} else {
a.next = current;
a = a.next;
}
} else {
if (b == null ) {
bRef[ 0 ] = current;
b = current;
} else {
b.next = current;
b = b.next;
}
}
current = current.next;
count++;
}
if (a != null ) {
a.next = null ;
}
if (b != null ) {
b.next = null ;
}
}
static void printList(Node node) {
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
public static void main(String[] args)
{
Node head = null ;
for ( int i = 5 ; i >= 0 ; i--) {
Node newNode = new Node(i);
newNode.next = head;
head = newNode;
}
System.out.print( "Original linked List: " );
printList(head);
Node[] aRef = new Node[ 1 ];
Node[] bRef = new Node[ 1 ];
AlternatingSplit(head, aRef, bRef);
System.out.print( "\nResultant Linked List 'a': " );
printList(aRef[ 0 ]);
System.out.print( "\nResultant Linked List 'b': " );
printList(bRef[ 0 ]);
}
}
|
Python
class Node:
def __init__( self , data, next = None ):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def AlternatingSplit( self , a, b):
first = self .head
second = first. next
while (first is not None and
second is not None and
first. next is not None ):
self .MoveNode(a, first)
self .MoveNode(b, second)
first = first. next . next
if first is None :
break
second = first. next
def MoveNode( self , dest, node):
new_node = Node(node.data)
if dest.head is None :
dest.head = new_node
else :
new_node. next = dest.head
dest.head = new_node
def push( self , data):
new_node = Node(data)
new_node. next = self .head
self .head = new_node
def printList( self ):
temp = self .head
while temp:
print temp.data,
temp = temp. next
print ("")
if __name__ = = "__main__" :
llist = LinkedList()
a = LinkedList()
b = LinkedList()
llist.push( 5 )
llist.push( 4 )
llist.push( 3 )
llist.push( 2 )
llist.push( 1 )
llist.push( 0 )
llist.AlternatingSplit(a, b)
print "Original Linked List: " ,
llist.printList()
print "Resultant Linked List 'a' : " ,
a.printList()
print "Resultant Linked List 'b' : " ,
b.printList()
|
C#
using System;
using System.Collections.Generic;
public class Node{
public int data;
public Node next;
public Node( int item){
data = item;
next = null ;
}
}
public class LinkedList{
Node head;
public void AlternatingSplit(LinkedList a, LinkedList b){
Node first = head;
Node second = first.next;
while (first != null && second != null && first.next != null )
{
MoveNode(a, first);
MoveNode(b, second);
first = first.next.next;
if (first == null )
break ;
second = first.next;
}
}
public void MoveNode(LinkedList dest, Node node){
Node new_node = new Node(node.data);
if (dest.head == null )
dest.head = new_node;
else {
new_node.next = dest.head;
dest.head = new_node;
}
}
void push( int data){
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
public void printList(){
Node temp = head;
while (temp != null ){
Console.Write(temp.data + " " );
temp = temp.next;
}
Console.WriteLine( "" );
}
public static void Main( string [] args){
LinkedList llist = new LinkedList();
LinkedList a = new LinkedList();
LinkedList b = new LinkedList();
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
llist.push(1);
llist.push(0);
llist.AlternatingSplit(a, b);
Console.WriteLine( "Original Linked List : " );
llist.printList();
Console.WriteLine( "Resultant Linked List 'a' : " );
a.printList();
Console.WriteLine( "Resultant Linked List 'b' : " );
b.printList();
}
}
|
Javascript
<script>
class Node{
constructor(data,next = null ){
this .data = data
this .next = next
}
}
class LinkedList
{
constructor()
{
this .head = null
}
AlternatingSplit(a, b){
let first = this .head
let second = first.next
while (first != null &&
second != null &&
first.next != null ){
this .MoveNode(a, first)
this .MoveNode(b, second)
first = first.next.next
if (first == null )
break
second = first.next
}
}
MoveNode(dest, node){
let new_node = new Node(node.data)
if (dest.head == null )
dest.head = new_node
else {
new_node.next = dest.head
dest.head = new_node
}
}
push(data){
let new_node = new Node(data)
new_node.next = this .head
this .head = new_node
}
printList(){
let temp = this .head
while (temp){
document.write(temp.data, " " );
temp = temp.next
}
document.write( "</br>" )
}
}
let llist = new LinkedList()
let a = new LinkedList()
let b = new LinkedList()
llist.push(5)
llist.push(4)
llist.push(3)
llist.push(2)
llist.push(1)
llist.push(0)
llist.AlternatingSplit(a, b)
document.write( "Original Linked List: " );
llist.printList()
document.write( "Resultant Linked List 'a' : " );
a.printList()
document.write( "Resultant Linked List 'b' : " );
b.printList()
</script>
|
Output:
Original linked List: 0 1 2 3 4 5
Resultant Linked List 'a' : 4 2 0
Resultant Linked List 'b' : 5 3 1
Time Complexity: O(n)
where n is a number of nodes in the given linked list.
Auxiliary Space: O(1)
As constant extra space is used.
Method 2(Using Dummy Nodes)
Here is an alternative approach that builds the sub-lists in the same order as the source list. The code uses temporary dummy header nodes for the ‘a’ and ‘b’ lists as they are being built. Each sublist has a “tail” pointer that points to its current last node — that way new nodes can be appended to the end of each list easily. The dummy nodes give the tail pointers something to point to initially. The dummy nodes are efficient in this case because they are temporary and allocated in the stack. Alternately, local “reference pointers” (which always point to the last pointer in the list instead of to the last node) could be used to avoid Dummy nodes.
C++
void AlternatingSplit(Node* source,
Node** aRef, Node** bRef)
{
Node aDummy;
Node* aTail = &aDummy;
Node bDummy;
Node* bTail = &bDummy;
Node* current = source;
aDummy.next = NULL;
bDummy.next = NULL;
while (current != NULL)
{
MoveNode(&(aTail->next), ¤t);
aTail = aTail->next;
if (current != NULL)
{
MoveNode(&(bTail->next), ¤t);
bTail = bTail->next;
}
}
*aRef = aDummy.next;
*bRef = bDummy.next;
}
|
C
void AlternatingSplit( struct Node* source, struct Node** aRef,
struct Node** bRef)
{
struct Node aDummy;
struct Node* aTail = &aDummy;
struct Node bDummy;
struct Node* bTail = &bDummy;
struct Node* current = source;
aDummy.next = NULL;
bDummy.next = NULL;
while (current != NULL)
{
MoveNode(&(aTail->next), ¤t);
aTail = aTail->next;
if (current != NULL)
{
MoveNode(&(bTail->next), ¤t);
bTail = bTail->next;
}
}
*aRef = aDummy.next;
*bRef = bDummy.next;
}
|
Java
static void AlternatingSplit(Node source, Node aRef,
Node bRef)
{
Node aDummy = new Node();
Node aTail = aDummy;
Node bDummy = new Node();
Node bTail = bDummy;
Node current = source;
aDummy.next = null ;
bDummy.next = null ;
while (current != null )
{
MoveNode((aTail.next), current);
aTail = aTail.next;
if (current != null )
{
MoveNode((bTail.next), current);
bTail = bTail.next;
}
}
aRef = aDummy.next;
bRef = bDummy.next;
}
|
Python3
def AlternatingSplit(source, aRef, bRef):
aDummy = Node();
aTail = aDummy;
bDummy = Node();
bTail = bDummy;
current = source;
aDummy. next = None ;
bDummy. next = None ;
while (current ! = None ):
MoveNode((aTail. next ), current);
aTail = aTail. next ;
if (current ! = None ):
MoveNode((bTail. next ), current);
bTail = bTail. next ;
aRef = aDummy. next ;
bRef = bDummy. next ;
|
C#
static void AlternatingSplit(Node source, Node aRef,
Node bRef)
{
Node aDummy = new Node();
Node aTail = aDummy;
Node bDummy = new Node();
Node bTail = bDummy;
Node current = source;
aDummy.next = null ;
bDummy.next = null ;
while (current != null )
{
MoveNode((aTail.next), current);
aTail = aTail.next;
if (current != null )
{
MoveNode((bTail.next), current);
bTail = bTail.next;
}
}
aRef = aDummy.next;
bRef = bDummy.next;
}
|
Javascript
<script>
function AlternatingSplit( source, aRef,
bRef)
{
var aDummy = new Node();
var aTail = aDummy;
var bDummy = new Node();
var bTail = bDummy;
var current = source;
aDummy.next = null ;
bDummy.next = null ;
while (current != null )
{
MoveNode((aTail.next), current);
aTail = aTail.next;
if (current != null )
{
MoveNode((bTail.next), current);
bTail = bTail.next;
}
}
aRef = aDummy.next;
bRef = bDummy.next;
}
</script>
|
Time Complexity: O(n)
where n is number of node in the given linked list.
Auxiliary Space: O(1)
As Constant extra space is used.
Source: http://cslibrary.stanford.edu/105/LinkedListProblems.pdf
Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem.