Javascript Program For Alternating Split Of A Given Singly Linked List- Set 1 Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like 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 // Node class to create a new node in the linked list class Node { constructor(data) { this.data = data; this.next = null; } } // Linked List class class LinkedList { constructor() { this.head = null; } // Method to add a new node at the end of the list append(data) { let newNode = new Node(data); if (this.head === null) { this.head = newNode; } else { let current = this.head; while (current.next !== null) { current = current.next; } current.next = newNode; } } // Method to print the linked list printList() { let current = this.head; let result = ''; while (current !== null) { result += current.data + ' '; current = current.next; } console.log(result.trim()); } } // Function to move the front node from the source list to the destination list function MoveNode(destRef, sourceRef) { if (sourceRef.head === null) return; // Take the node from the front of the source let newNode = sourceRef.head; sourceRef.head = sourceRef.head.next; // Move the node to the front of the destRef list newNode.next = destRef.head; destRef.head = newNode; } // Function to split the linked list into two alternating lists function AlternatingSplit(source, aRef, bRef) { let current = source.head; // Alternate between adding nodes to 'aRef' and 'bRef' while (current !== null) { // Move the node to 'aRef' MoveNode(aRef, source); // If the current node is not null, move the next node to 'bRef' if (source.head !== null) { MoveNode(bRef, source); } current = source.head; // Update current to the new head of the source } } // Create a linked list let sourceList = new LinkedList(); // Add nodes to the source list sourceList.append(1); sourceList.append(2); sourceList.append(3); sourceList.append(4); sourceList.append(5); sourceList.append(6); // Create references for the new lists let aRef = new LinkedList(); let bRef = new LinkedList(); // Print the original list console.log("Original list:"); sourceList.printList(); // Perform alternating split AlternatingSplit(sourceList, aRef, bRef); // Print the split lists console.log("List A:"); aRef.printList(); console.log("List B:"); bRef.printList(); OutputOriginal list: 1 2 3 4 5 6 List A: 5 3 1 List B: 6 4 2 Complexity Analysis: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.Please refer complete article on Alternating split of a given Singly Linked List | Set 1 for more details! Comment More info K kartik Follow Improve Article Tags : JavaScript Linked Lists Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like