C++ Program For Alternating Split Of A Given Singly Linked List- Set 1
Last Updated :
11 Apr, 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++
/* C++ Program to alternatively split
a linked list into two halves */
#include <bits/stdc++.h>
using namespace std;
// Link list node
class Node
{
public:
int data;
Node* next;
};
/* Pull off the front node of
the source and put it in dest */
void MoveNode(Node** destRef,
Node** sourceRef) ;
/* Given the source list, split its nodes
into two shorter lists. If we number the
elements 0, 1, 2, ... then all the even
elements should go in the first list, and
all the odd elements in the second. The
elements in the new lists may be in any order. */
void AlternatingSplit(Node* source,
Node** aRef,
Node** bRef)
{
/* Split the nodes of source
to these 'a' and 'b' lists */
Node* a = NULL;
Node* b = NULL;
Node* current = source;
while (current != NULL)
{
// Move a node to list 'a'
MoveNode(&a, &t);
if (current != NULL)
{
// Move a node to list 'b'
MoveNode(&b, &t);
}
}
*aRef = a;
*bRef = b;
}
/* Take the node from the front of
the source, and move it to the front
of the dest. It is an error to call
this with the source list empty.
Before calling MoveNode():
source == {1, 2, 3}
dest == {1, 2, 3}
After calling MoveNode():
source == {2, 3}
dest == {1, 1, 2, 3} */
void MoveNode(Node** destRef,
Node** sourceRef)
{
// The front source node
Node* newNode = *sourceRef;
assert(newNode != NULL);
// Advance the source pointer
*sourceRef = newNode->next;
// Link the old dest off the
// new node
newNode->next = *destRef;
// Move dest to point to the
// new node
*destRef = newNode;
}
// Utility Functions
/* Function to insert a node at
the beginning of the linked list */
void push(Node** head_ref,
int new_data)
{
// Allocate node
Node* new_node = new Node();
// Put in the data
new_node->data = new_data;
// Link the old list of the
// new node
new_node->next = (*head_ref);
// Move the head to point to the
// new node
(*head_ref) = new_node;
}
/* Function to print nodes
in a given linked list */
void printList(Node *node)
{
while(node != NULL)
{
cout << node->data << " ";
node = node->next;
}
}
// Driver code
int main()
{
// Start with the empty list
Node* head = NULL;
Node* a = NULL;
Node* b = NULL;
/* Let us create a sorted linked list
to test the functions
Created linked list will be
0->1->2->3->4->5 */
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
push(&head, 0);
cout << "Original linked List: ";
printList(head);
// Remove duplicates from linked list
AlternatingSplit(head, &a, &b);
cout << "Resultant Linked List 'a' : ";
printList(a);
cout << "Resultant Linked List 'b' : ";
printList(b);
return 0;
}
// This code is contributed by rathbhupendra
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)
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;
// Points to the last node in 'a'
Node* aTail = &aDummy;
Node bDummy;
// Points to the last node in 'b'
Node* bTail = &bDummy;
Node* current = source;
aDummy.next = NULL;
bDummy.next = NULL;
while (current != NULL)
{
// Add at 'a' tail
MoveNode(&(aTail->next), &t);
// Advance the 'a' tail
aTail = aTail->next;
if (current != NULL)
{
MoveNode(&(bTail->next), ¤t);
bTail = bTail->next;
}
}
*aRef = aDummy.next;
*bRef = bDummy.next;
}
// This code is contributed by rathbhupendra
Time Complexity: O(n) where n is number of node in the given linked list.
Space Complexity: O(n) as the function creates 2 new linked lists.
Source: http://cslibrary.stanford.edu/105/LinkedListProblems.pdf Please refer complete article on Alternating split of a given Singly Linked List | Set 1 for more details!
Similar Reads
Javascript Program For Alternating Split Of A Given Singly Linked List- Set 1 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
3 min read
Menu driven program for all operations on singly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.Operations to be PerformedcreateList(): To create the list with the
8 min read
Rearrange a Linked List in Zig-Zag fashion | Set-2 Given a linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f .. where a, b, c.. are consecutive data node of linked list. Note that it is not allowed to swap data. Examples: Input: 1->2->3->4 Output: 1->3->2->4 Input: 11->
13 min read
Pairwise Swap Nodes of a given linked list by changing links Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7, and if the linked list is 1->2->3->4->5->6 then the function sh
15+ min read
Reverse the order of all nodes at even position in given Linked List Given a linked list A[] of N integers, the task is to reverse the order of all integers at an even position. Examples: Input: A[] = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: 1 6 3 4 5 2Explanation: Nodes at even position in the given linked list are 2, 4 and 6. So, after reversing
10 min read
Partition a Linked List into K continuous groups with difference in their sizes at most 1 Given a linked list consisting of n nodes and an integer k, the task is to split the given Linked List into k continuous groups such that the difference between the size of the adjacent groups after splitting is at most 1 and the groups are sorted in descending order of their lengths. Note: A group
9 min read