C Program To Merge A Linked List Into Another Linked List At Alternate Positions Last Updated : 31 Mar, 2022 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. 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 <stdio.h> #include <stdlib.h> // A nested list node struct Node { int data; struct Node *next; }; /* Function to insert a node at the beginning */ void push(struct Node ** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct 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(struct Node *head) { struct Node *temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf(""); } // 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(struct Node *p, struct Node **q) { struct Node *p_curr = p, *q_curr = *q; struct 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() { struct Node *p = NULL, *q = NULL; push(&p, 3); push(&p, 2); push(&p, 1); printf( "First Linked List:"); printList(p); push(&q, 8); push(&q, 7); push(&q, 6); push(&q, 5); push(&q, 4); printf( "Second Linked List:"); printList(q); merge(p, &q); printf( "Modified First Linked List:"); printList(p); printf( "Modified Second Linked List:"); printList(q); getchar(); return 0; } 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! Comment More infoAdvertise with us Next Article C Program To Merge A Linked List Into Another Linked List At Alternate Positions 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 Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 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 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 To Delete N Nodes After M Nodes Of A Linked List Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.Difficulty Level: Rookie Examples: Input: M = 2, N = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked L 3 min read Program for all operations on Circular Linked List in C In a Circular linked list, every element has a link to its next element in the sequence, and the last element has a link to the first element. A circular linked list is similar to the singly linked list except that the last node points to the first node. Below is the image to illustrate the same: 1. 11 min read Menu driven program for all operations on doubly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. A Doubly Linked List in contains three parts: one is the data part and the other two are the address of the next and previous node in the list. In this article, all the common 5 min read C Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7, and if the linked list is 1->2->3->4->5->6 then the function sh 5 min read C Program For Finding The Middle Element Of A Given Linked List Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if given linked list 4 min read Like