[Expected Approach - 1] Using Recursion - O(n) Time and O(n) Space:
To convert a singly linked list into a circular linked list using recursion, begin by recursively traversing the list to reach the last node, which is the node whose next pointer is nullptr. Update last node's next pointer to point back to the head node to form a circular link.
Below is the implementation of the above approach:
Output
10 12 14 16
Time Complexity: O(n), where n is the number of nodes in the list. Auxiliary Space: O(n)
[Expected Approach - 2] Using Iterative Approach - O(n) Time and O(1) Space:
To convert a singly linked list into a circular linked list iteratively, start by initializing a pointer to traverse the list from the head node. Traverse the list until we reach the node whose next pointer that is nullptr. Update its next pointer to point back to the head node.
Below is the implementation of the above approach:
Output
10 12 14 16
Time Complexity: O(n), where n is the number of nodes in the list. Auxiliary Space: O(1)