Alternating split of a given Singly Linked List
Method 2(Using Dummy Nodes)
Here is an alternative approach which builds the sub-lists in the same order as the source list. The code uses a temporary dummy header nodes for the ‘a’ and ‘b’ lists as they are being built.
Read full article from Alternating split of a given Singly Linked List | GeeksforGeeks
Method 2(Using Dummy Nodes)
Here is an alternative approach which builds the sub-lists in the same order as the source list. The code uses a temporary dummy header nodes for the ‘a’ and ‘b’ lists as they are being built.
void AlternatingSplit(struct node* source, struct node** aRef, struct node** bRef) { struct node aDummy; struct node* aTail = &aDummy; /* points to the last node in 'a' */ struct node bDummy; struct node* bTail = &bDummy; /* points to the last node in 'b' */ struct node* current = source; aDummy.next = NULL; bDummy.next = NULL; while (current != NULL) { MoveNode(&(aTail->next), ¤t); /* add at 'a' tail */ aTail = aTail->next; /* advance the 'a' tail */ if (current != NULL) { MoveNode(&(bTail->next), ¤t); bTail = bTail->next; } } *aRef = aDummy.next; *bRef = bDummy.next;}void MoveNode(struct node** destRef, struct node** sourceRef) { /* the front source node */ struct 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; }