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 list
1->2->3->4->NULL
Output:
Original list: 1 -> 2 -> 3 -> 4 -> NULL
Duplicate list: 1 -> 2 -> 3 -> 4 -> NULLInput: Head of following linked list
1->2->3->4->5->NULL
Output:
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; } |
Original list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Duplicate list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- C Program to reverse each node value in Singly Linked List
- Program to delete all even nodes from a Singly Linked List
- Create new linked list from two given linked list with greater element at each node
- Implement a stack using singly linked list
- Create a Circular List Structure For Given Value K Using Recursion
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Function to check if a singly linked list is palindrome
- Alternating split of a given Singly Linked List | Set 1
- Reverse alternate K nodes in a Singly Linked List
- QuickSort on Singly Linked List
- Select a Random Node from a Singly Linked List
- Circular Singly Linked List | Insertion
- Insertion Sort for Singly Linked List
- Recursive selection sort for singly linked list | Swapping node links
- Alternate Odd and Even Nodes in a Singly Linked List
- Find middle of singly linked list Recursively
- Binary Search on Singly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

