C++ Program To Merge A Linked List Into Another Linked List At Alternate Positions Last Updated : 16 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two linked lists, insert nodes of second list into first list at alternate positions of 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 second list should only be inserted when there are positions available. For example, if the first list is 1->2->3 and second list is 4->5->6->7->8, then first list should become 1->4->2->5->3->6 and 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. Expected time complexity is O(n) where n is number of nodes in first list. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. 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. C++ // C++ program to merge a linked list // into another at alternate positions #include <bits/stdc++.h> using namespace std; // A nexted list node class Node { public: int data; Node *next; }; /* Function to insert a node at the beginning */ void push(Node ** head_ref, int new_data) { Node* new_node = new Node(); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } /* Utility function to print a singly linked list */ void printList(Node *head) { Node *temp = head; while (temp != NULL) { cout << temp -> data << " "; temp = temp -> next; } cout << endl; } // 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. void merge(Node *p, Node **q) { Node *p_curr = p, *q_curr = *q; Node *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; } // Driver code int main() { Node *p = NULL, *q = NULL; push(&p, 3); push(&p, 2); push(&p, 1); cout << "First Linked List:"; printList(p); push(&q, 8); push(&q, 7); push(&q, 6); push(&q, 5); push(&q, 4); cout << "Second Linked List:"; printList(q); merge(p, &q); cout << "Modified First Linked List:"; printList(p); cout << "Modified Second Linked List:"; printList(q); return 0; } // This code is contributed by rathbhupendra 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(min(n1, n2)), where n1 and n2 represents the length of the given two linked lists.Auxiliary Space: O(1), no extra space is required, so it is a constant. 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 C++ Program For Moving All Occurrences Of An Element To End In A Linked List K kartik Follow Improve Article Tags : Linked List C++ Programs DSA Amazon Practice Tags : AmazonLinked List Similar Reads C++ Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read C++ Program For In-Place Merge Two Linked Lists Without Changing Links Of First List Given two sorted singly linked lists having n and m elements each, merge them using constant space. First, n smallest elements in both the lists should become part of the first list and the rest elements should be part of the second list. Sorted order should be maintained. We are not allowed to chan 4 min read C++ Program For Moving Last Element To Front Of A Given Linked List Write a function that moves the last element to the front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4. Algorithm: Traverse the list till the last node. Use two pointers: one t 3 min read C++ Program For Pointing To Next Higher Value Node In A Linked List With An Arbitrary Pointer Given singly linked list with every node having an additional "arbitrary" pointer that currently points to NULL. Need to make the "arbitrary" pointer point to the next higher value node. We strongly recommend to minimize your browser and try this yourself first A Simple Solution is to traverse all n 5 min read C++ Program For Moving All Occurrences Of An Element To End In A Linked List Given a linked list and a key in it, the task is to move all occurrences of the given key to the end of the linked list, keeping the order of all other elements the same. Examples: Input : 1 -> 2 -> 2 -> 4 -> 3 key = 2 Output : 1 -> 4 -> 3 -> 2 -> 2 Input : 6 -> 6 -> 7 6 min read C++ 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 Like