C 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 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<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 ("
Original linked List: ");
printList(head);
AlternatingSplit(head, &a, &b);
printf ("
Resultant Linked List 'a' ");
printList(a);
printf ("
Resultant Linked List 'b' ");
printList(b);
getchar ();
return 0;
}
|
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.
Space Complexity: O(1),The algorithm only uses two pointers, thus it has a constant space complexity of 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( 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;
}
|
Time Complexity: O(n) where n is number of node in the given linked list
Space Complexity: O(1) ,we are not using any extra space.
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!
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...