C program to create copy of a singly Linked List using Recursion Last Updated : 20 Oct, 2020 Comments Improve Suggest changes Like Article Like Report Given a pointer to the head node of a Linked List, the task is to create a copy of the linked list using recursion. Examples:: Input: Head of following linked list1->2->3->4->NULLOutput: Original list: 1 -> 2 -> 3 -> 4 -> NULLDuplicate list: 1 -> 2 -> 3 -> 4 -> NULL Input: Head of following linked list1->2->3->4->5->NULLOutput: Original list: 1->2->3->4->5->NULL, Duplicate list: 1->2->3->4->5->NULL, Approach: Follow the steps below to solve the problem: Base case: if (head == NULL), then return NULL. Allocate the new Node in the Heap using malloc() & set its data.Recursively set the next pointer of the new Node by recurring for the remaining nodes. Return the head pointer of the duplicate node. Finally, print both the original linked list and the duplicate linked list. Below is the implementation of the above approach: C // C program for the above approach #include <stdio.h> #include <stdlib.h> // Node for linked list struct Node { int data; struct Node* next; }; // Function to print given linked list void printList(struct Node* head) { struct Node* ptr = head; while (ptr) { printf("%d -> ", ptr->data); ptr = ptr->next; } printf("NULL"); } // Function to create a new node void insert(struct Node** head_ref, int data) { // Allocate the memory for new Node // in the heap and set its data struct Node* newNode = (struct Node*)malloc( sizeof(struct Node)); newNode->data = data; // Set the next node pointer of the // new Node to point to the current // node of the list newNode->next = *head_ref; // Change the pointer of head to point // to the new Node *head_ref = newNode; } // Function to create a copy of a linked list struct Node* copyList(struct Node* head) { if (head == NULL) { return NULL; } else { // Allocate the memory for new Node // in the heap and set its data struct Node* newNode = (struct Node*)malloc( sizeof(struct Node)); newNode->data = head->data; // Recursively set the next pointer of // the new Node by recurring for the // remaining nodes newNode->next = copyList(head->next); return newNode; } } // Function to create the new linked list struct Node* create(int arr[], int N) { // Pointer to point the head node // of the singly linked list struct Node* head_ref = NULL; // Construct the linked list for (int i = N - 1; i >= 0; i--) { insert(&head_ref, arr[i]); } // Return the head pointer return head_ref; } // Function to create both the lists void printLists(struct Node* head_ref, struct Node* dup) { printf("Original list: "); // Print the original linked list printList(head_ref); printf("\nDuplicate list: "); // Print the duplicate linked list printList(dup); } // Driver Code int main(void) { // Given nodes value int arr[] = { 1, 2, 3, 4, 5 }; int N = sizeof(arr) / sizeof(arr[0]); // Head of the original Linked list struct Node* head_ref = create(arr, N); // Head of the duplicate Linked List struct Node* dup = copyList(head_ref); printLists(head_ref, dup); return 0; } Output: Original list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Duplicate list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Time Complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article C program to create copy of a singly Linked List using Recursion R Rajput-Ji Follow Improve Article Tags : Linked List Recursion C Programs DSA C-Pointers +1 More Practice Tags : Linked ListRecursion Similar Reads C Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, 3 min read C Program For Reversing A Linked List In Groups Of Given Size - Set 1 Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, 3 min read C Program For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit 2 min read C# Program For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit 2 min read C Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to th 2 min read C# Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to th 4 min read C Program To Add Two Numbers Represented By Linked Lists- Set 1 Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5->6->3 // represents number 563 List2: 8->4->2 // represents number 842 Output: Resultant list: 1- 4 min read 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 to Reverse a Stack using Recursion Write a program to reverse a stack using recursion, without using any loop. Example:Â Input: elements present in stack from top to bottom 1 2 3 4Â Output: 4 3 2 1Â Input: elements present in stack from top to bottom 1 2 3Output: 3 2 1 Recommended PracticeReverse a StackTry It!Reverse a stack using Re 5 min read Like