Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Javascript Program For Alternating Split Of A Given Singly Linked List- Set 1

  • Last Updated : 16 Dec, 2021

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 (Using Dummy Nodes): 
Here is an 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.


// Javascript program to implement the 
// above approach
function AlternatingSplit(source, aRef, 
    var aDummy = new Node();
    // Points to the last node in 'a' 
    var aTail = aDummy; 
    var bDummy = new Node();
    // Points to the last node in 'b' 
    var bTail = bDummy; 
    var current = source; = null; = null;
    while (current != null
        // Add at 'a' tail 
        // Advance the 'a' tail 
        aTail =; 
        if (current != null
            bTail =;
    aRef =;
    bRef =;
// This code is contributed by aashish1995

Time Complexity: O(n) where n is number of node in the given linked list.
Please refer complete article on Alternating split of a given Singly Linked List | Set 1 for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!