VOOZH about

URL: https://www.geeksforgeeks.org/dsa/c-program-to-create-copy-of-a-singly-linked-list-using-recursion/

⇱ C program to create copy of a singly Linked List using Recursion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C program to create copy of a singly Linked List using Recursion

Last Updated : 15 Jul, 2025

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 -> NULL

Input: 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:

  1. Base case: if (head == NULL), then return NULL.
  2. Allocate the new Node in the Heap using malloc() & set its data.
  3. Recursively set the next pointer of the new Node by recurring for the remaining nodes.
  4. Return the head pointer of the duplicate node.
  5. Finally, print both the original linked list and the duplicate linked list.

Below is the implementation of the above approach:

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