# 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 they occurred in the source list. Method 2 inserts the node at the end by keeping track of last node in sublists.

`/*Program to alternatively split a linked list into two halves */` `#include<stdio.h> ` `#include<stdlib.h> ` `#include<assert.h> ` ` ` `/* Link list node */` `struct` `Node ` `{ ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* pull off the front node of the source and put it in dest */` `void` `MoveNode(` `struct` `Node** destRef, ` `struct` `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(` `struct` `Node* source, ` `struct` `Node** aRef, ` ` ` `struct` `Node** bRef) ` `{ ` ` ` `/* split the nodes of source to these 'a' and 'b' lists */` ` ` `struct` `Node* a = NULL; ` ` ` `struct` `Node* b = NULL; ` ` ` ` ` `struct` `Node* current = source; ` ` ` `while` `(current != NULL) ` ` ` `{ ` ` ` `MoveNode(&a, ¤t); ` `/* Move a node to list 'a' */` ` ` `if` `(current != NULL) ` ` ` `{ ` ` ` `MoveNode(&b, ¤t); ` `/* Move a node to list 'b' */` ` ` `} ` ` ` `} ` ` ` `*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} ` ` ` ` ` `Affter calling MoveNode(): ` ` ` `source == {2, 3} ` ` ` `dest == {1, 1, 2, 3} ` `*/` `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; ` `} ` ` ` `/* UTILITY FUNCTIONS */` `/* Function to insert a node at the beginging of the linked list */` `void` `push(` `struct` `node** head_ref, ` `int` `new_data) ` `{ ` ` ` `/* allocate node */` ` ` `struct` `Node* new_node = ` ` ` `(` `struct` `Node*) ` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` ` ` `/* put in the data */` ` ` `new_node->data = new_data; ` ` ` ` ` `/* link the old list off 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(` `struct` `Node *node) ` `{ ` ` ` `while` `(node!=NULL) ` ` ` `{ ` ` ` `printf` `(` `"%d "` `, node->data); ` ` ` `node = node->next; ` ` ` `} ` `} ` ` ` `/* Drier program to test above functions*/` `int` `main() ` `{ ` ` ` `/* Start with the empty list */` ` ` `struct` `Node* head = NULL; ` ` ` `struct` `Node* a = NULL; ` ` ` `struct` `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); ` ` ` ` ` `printf` `(` `"\n Original linked List: "` `); ` ` ` `printList(head); ` ` ` ` ` `/* Remove duplicates from linked list */` ` ` `AlternatingSplit(head, &a, &b); ` ` ` ` ` `printf` `(` `"\n Resultant Linked List 'a' "` `); ` ` ` `printList(a); ` ` ` ` ` `printf` `(` `"\n Resultant Linked List 'b' "` `); ` ` ` `printList(b); ` ` ` ` ` `getchar` `(); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Time Complexity: O(n) where n is number of node in the given 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. Each sublist has a “tail” pointer which 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 points to the last pointer in the list instead of to the last node) could be used to avoid Dummy nodes.

`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; ` `} ` |

*chevron_right*

*filter_none*

Time Complexity: O(n) where n is number of node in the given linked list.

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.

## Recommended Posts:

- Find the middle of a given linked list in C and Java
- Program for n'th node from the end of a Linked List
- Write a function to get Nth node in a Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Detect loop in a linked list
- Write a function to delete a Linked List
- Write a function that counts the number of times a given int occurs in a Linked List
- Reverse a linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Write a function to get the intersection point of two Linked Lists.
- Function to check if a singly linked list is palindrome
- The Great Tree-List Recursion Problem.
- Clone a linked list with next and random pointer | Set 1
- Memory efficient doubly linked list
- Given a linked list which is sorted, how will you insert in sorted way