Open In App

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

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




<script>
// Javascript program to implement the
// above approach
function AlternatingSplit(source, aRef,
                          bRef)
{
    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;
    aDummy.next = null;
    bDummy.next = null;
    while (current != null)
    {
        // Add at 'a' tail
        MoveNode((aTail.next),
                  current);
 
        // Advance the 'a' tail
        aTail = aTail.next;
        if (current != null)
        {
            MoveNode((bTail.next),
                      current);
            bTail = bTail.next;
        }
    }
    aRef = aDummy.next;
    bRef = bDummy.next;
}
// This code is contributed by aashish1995
</script>


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

Space Complexity: O(1), because we just use some variables.
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!



Previous Article
Next Article

Similar Reads

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-&gt;1-&gt;0-&gt;1-&gt;0-&gt;1 then one sublist should be 0-&gt;0-&gt;0 and the other should be 1-&gt;1-&gt;1. Recommended: Pl
5 min read
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-&gt;1-&gt;0-&gt;1-&gt;0-&gt;1 then one sublist should be 0-&gt;0-&gt;0 and the other should be 1-&gt;1-&gt;1. Recommended: Pl
5 min read
Python 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-&gt;1-&gt;0-&gt;1-&gt;0-&gt;1 then one sublist should be 0-&gt;0-&gt;0 and the other should be 1-&gt;1-&gt;1. Recommended: Pl
3 min read
Java 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-&gt;1-&gt;0-&gt;1-&gt;0-&gt;1 then one sublist should be 0-&gt;0-&gt;0 and the other should be 1-&gt;1-&gt;1. Recommended: Pl
2 min read
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-&gt;1-&gt;0-&gt;1-&gt;0-&gt;1 then one sublist should be 0-&gt;0-&gt;0 and the other should be 1-&gt;1-&gt;1. Recommended Pra
17 min read
Recursive approach for alternating split of Linked List
Given a linked list, split the linked list into two with alternate nodes. Examples: Input : 1 2 3 4 5 6 7 Output : 1 3 5 7 2 4 6 Input : 1 4 5 6 Output : 1 5 4 6 We have discussed Iterative splitting of linked list. The idea is to begin from two nodes first and second. Let us call these nodes as 'a' and 'b'. We recurs Implementation: C/C++ Code //
7 min read
Convert Singly Linked List into Circular Linked List using JavaScript
We are given a singly linked list and we need to convert it into a circular linked list, where the last node points back to the head of the list. Converting a singly linked list into a circular linked list involves making the last node's next pointer point back to the head, creating a circular structure. This transformation enables traversal from t
3 min read
Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node. Introduction to Doubly linked list : A Doubly Linked List (D
2 min read
Convert Singly Linked List to XOR Linked List
Prerequisite: XOR Linked List – A Memory Efficient Doubly Linked List | Set 1XOR Linked List – A Memory Efficient Doubly Linked List | Set 2 An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node's address. Given a singly linked list, the task is to convert the gi
9 min read
When is Doubly Linked List more Efficient than Singly Linked List?
Did you know there are some cases where a Doubly Linked List is more efficient than a Singly Linked List, even though it takes more memory compared to a Singly Linked List? What are those Cases? Well, we will discuss that in the following article, But first, let's talk about Singly and linked lists: What is a Singly Linked List?A singly linked list
4 min read
Practice Tags :