# Alternating split of a given Singly Linked List in C++

In this article, we will discuss how to alter the split of a given singly linked list in C++ with its explanation and benefits.

Let's take an input of a singly linked list. Here, our main objective is to divide the list into two singly linked lists with different nodes from the original list. If the input list contains the nodes a, b, c, d, e, and f, two sub-lists will be a, c, e, and b, d, f after the split.

After that, we'll pick two pointers, N1 and N2, one of which points to the original list's head and the other to that same head. Shift both pointers to the subsequent node to build sublists.

### Examples:

Input - List :- 1 → 5 → 7 → 12 → 2 → 96 → 33

Output - Original List: 1 5 7 12 2 96 33

List 1: 1 7 2 33

List 2: 5 12 96

Explanation - Start from 1 and 5 and next point to alternate nodes to create sublists shown above.

Input - List :- 13 → 53 → 90 → 18 → 44 → 11→ 99 → 32

Output - Original List: 13 53 90 18 44 11 99 32

List 1: 13 90 44 99

List 2: 53 18 11 32

Explanation - Start from 13 and 53 and next point to alternate nodes to create sublists shown above.

## Simple Approach:

In this method, we'll use two pointers, N1 and N2, one pointing to the original list's head and the other to the head of the following list. Shift both pointers to the subsequent node to build sublists.

1. Consider the structure that has an int data component and a Node as the next pointer.
3. Create a singly linked list using the code mentioned earlier and a pointer to the first node as the head.
5. Consider the Node1 and Node2 pointers.
6. Node pointers are used in the splitList function, which points Node* head, Node** n1, and Node** n2 to the head and next of the original string.
7. Use split(*n1, *n2) inside it to divide the original list into two sublists.
8. The split(Node* N1, Node* N2) function accepts two pointers, N1 and N2, and generates two sublists, each of which contains alternating nodes from the original list.
9. Return nothing if N1 and N2 are both null.
10. If N1's next is not null, set tmp=N1->next->next and set N1->next to the current time; if N2's next is not null, set tmp=N2->next->next and set N2->next to the current time; call split(N1->next, N2->next); for the following iteration.
11. Print sublists using display() function at the conclusion.

### Program:

Let's take an example to illustrate how to alter the split of a given singly linked list in C++.

Output:

## Benefits of Alternating split of a given Singly Linked List

A single linked list is often split into two alternating portions by splitting it into two different lists, one of which contains nodes at even places and the other of which contains nodes at odd positions. We can achieve this by going through the first list several times and making two separate lists for odd and even spots. The advantages of splitting a single linked list in C++ alternately include the following:

1. Better Data Organization: We can more logically arrange the data by dividing the list into two sublists. When working with items in a certain pattern or needing to conduct actions on odd and even elements individually, it can be quite helpful.
2. Data Processing Simplified: Alternating split can make some data processing operations simpler. For instance, separating the list can make processing easier by isolating the alternating properties of the items (for example, students having roll numbers and names).
3. Parallelism: Processing odd and even entries of a list in parallel may be desirable in some circumstances. In multi-core computers, splitting the list into odd and even portions can enhance speed by facilitating parallel processing.
4. Algorithmic Practice: Dividing a linked list into alternating sections is a good practice for manipulating and traversing linked lists. The ability to work with linked data structures will improve as a result.
5. Modular Code: Create functions or methods to carry out the alternating split to increase the modularity and maintainability of our code. This separation of concerns can enhance the readability and reuse of code.
6. Enhanced Testing: Dividing the list into two sections makes it simpler to test and troubleshoot particular activities on each portion. We can concentrate on testing the reasoning behind even-positioned nodes apart from the reasoning behind odd-positioned nodes.
7. Flexibility: Flexibility in data handling and manipulation is made possible by the capability to divide a singly linked list into alternating sections. We can modify the code to comply with different data patterns and specifications.