Javascript Program To Merge A Linked List Into Another Linked List At Alternate Positions Last Updated : 03 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given two linked lists, insert nodes of the second list into the first list at alternate positions of the first list. For example, if first list is 5->7->17->13->11 and second is 12->10->2->4->6, the first list should become 5->12->7->10->17->2->13->4->11->6 and second list should become empty. The nodes of the second list should only be inserted when there are positions available. For example, if the first list is 1->2->3 and the second list is 4->5->6->7->8, then the first list should become 1->4->2->5->3->6 and the second list to 7->8.Use of extra space is not allowed (Not allowed to create additional nodes), i.e., insertion must be done in-place. The expected time complexity is O(n) where n is a number of nodes in the first list. The idea is to run a loop while there are available positions in first loop and insert nodes of second list by changing pointers. Following are implementations of this approach. JavaScript // Javascript program to merge a linked list // into another at alternate positions // A nexted list node class Node { constructor() { this.data = 0; this.next = null; } }; /* Function to insert a node at the beginning */ function push(head_ref, new_data) { let new_node = new Node(); new_node.data = new_data; new_node.next = (head_ref); (head_ref) = new_node; return head_ref; } /* Utility function to print a singly linked list */ function printList(head) { let temp = head; while (temp != null) { console.log(temp.data + " "); temp = temp.next; } } // Main function that inserts nodes of // linked list q into p at alternate // positions. Since head of first list // never changes and head of second list // may change, we need single pointer for // first list and double pointer for second // list. function merge(p, q) { let p_curr = p, q_curr = q; let p_next, q_next; // While there are available positions // in p while (p_curr != null && q_curr != null) { // Save next pointers p_next = p_curr.next; q_next = q_curr.next; // Make q_curr as next of p_curr // Change next pointer of q_curr q_curr.next = p_next; // Change next pointer of p_curr p_curr.next = q_curr; // Update current pointers for next // iteration p_curr = p_next; q_curr = q_next; } // Update head pointer of second list q = q_curr; return q; } // Driver code let p = null, q = null; p = push(p, 3); p = push(p, 2); p = push(p, 1); console.log( "First Linked List:"); printList(p); q = push(q, 8); q = push(q, 7); q = push(q, 6); q = push(q, 5); q = push(q, 4); console.log( "Second Linked List:"); printList(q); q = merge(p, q); console.log( "Modified First Linked List:"); printList(p); console.log( "Modified Second Linked List:"); printList(q); // This code is contributed by rrrtnx. OutputFirst Linked List: 1 2 3 Second Linked List: 4 5 6 7 8 Modified First Linked List: 1 4 2 5 3 6 Modified Second Linked List: 7 8 Complexity Analysis:Time Complexity: O(N)Auxiliary Space: O(1)Please refer complete article on Merge a linked list into another linked list at alternate positions for more details! Comment More infoAdvertise with us Next Article Javascript Program To Merge A Linked List Into Another Linked List At Alternate Positions kartik Follow Improve Article Tags : Linked List JavaScript Web Technologies DSA Amazon +1 More Practice Tags : AmazonLinked List Similar Reads Merge a linked list into another linked list at alternate positions Given two singly linked lists, The task is to insert nodes of the second list into the first list at alternate positions of the first list and leave the remaining nodes of the second list if it is longer.Example:Input: head1: 1->2->3 , head2: 4->5->6->7->8Output: head1: 1->4- 8 min read Javascript Program For Merge Sort Of Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Let the head be the first node of the linked list to be sorted and headRef 6 min read Javascript Program To Merge K Sorted Linked Lists Using Min Heap - Set 2 Given k linked lists each of size n and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output.Examples: Input: k = 3, n = 4list1 = 1->3->5->7->NULLlist2 = 2->4->6->8->NULLlist 5 min read Javascript Program For Making Middle Node Head In A Linked List Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples:Input: 1 2 3 4 5 Output: 3 1 2 4 5Input: 1 2 3 4 5 6Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves on 3 min read Insert a whole linked list into other at k-th position Given two linked lists and a number k. Insert second linked list in to first at k-th position Examples: Input : a : 1->2->3->4->5->NULL b : 7->8->9->10->11->NULL k = 2 Output :1->2->7->8->9->10->11->3->4->5->NULL Input : a: 10->15->20 10 min read Javascript Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810Merge sort for singly linked list is already discussed. The important change here is to modify the previous pointer 3 min read Javascript Program For Partitioning A Linked List Around A Given Value And Keeping The Original Order Given a linked list and a value x, partition it such that all nodes less than x come first, then all nodes with a value equal to x, and finally nodes with a value greater than or equal to x. The original relative order of the nodes in each of the three partitions should be preserved. The partition m 4 min read Merge Sort for Linked Lists in JavaScript Prerequisite: Merge Sort for Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. In this post, Merge sort for lin 4 min read Javascript Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. JavaScript// Linked List Class // Head of list let head; // Node 7 min read Javascript Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where 7 min read Like