Tuesday, November 26, 2024
Google search engine
HomeData Modelling & AIJavascript Program To Merge A Linked List Into Another Linked List At...

Javascript Program To Merge A Linked List Into Another Linked List At Alternate Positions

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




<script>
// 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)
{
    var 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)
{
    var temp = head;
    while (temp != null)
    {
        document.write( temp.data + " ");
        temp = temp.next;
    }
    document.write("<br>");
}
 
// 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)
{
    var p_curr = p, q_curr = q;
    var 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
var p = null, q = null;
p = push(p, 3);
p = push(p, 2);
p = push(p, 1);
document.write(
         "First Linked List:<br>");
printList(p);
q = push(q, 8);
q = push(q, 7);
q = push(q, 6);
q = push(q, 5);
q = push(q, 4);
document.write(
         "Second Linked List:<br>");
printList(q);
q = merge(p, q);
document.write(
         "Modified First Linked List:<br>");
printList(p);
document.write(
         "Modified Second Linked List:<br>");
printList(q);
// This code is contributed by rrrtnx.
</script>


Output: 

First 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 

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!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments