Posted on by Kalkicode

The problem involves converting a singly linked list into a circular linked list. In a singly linked list, each node points to the next node in the list. In a circular linked list, the last node's next pointer points back to the first node, forming a loop.

## Problem Statement

Given a singly linked list, the task is to convert it into a circular linked list by making the last node's next pointer point to the head of the list.

## Example

Consider the singly linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7. After converting it into a circular linked list, the last node's next pointer will point to the first node, creating a loop: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 1.

## Idea to Solve the Problem

To convert a singly linked list into a circular linked list, we need to update the next pointer of the last node to point back to the head node. This forms a loop in the linked list and makes it circular.

## Pseudocode

Here's the pseudocode to convert a singly linked list into a circular linked list:

``````function makeCircular():
else:
while temp.next is not NULL:
temp = temp.next
// The list is already circular
return
// Connect the last node to the head node

## Algorithm Explanation

1. Check if the linked list is empty. If it's empty, print "Empty Linked List."
2. Initialize `temp` to the head node and use a loop to traverse the linked list until the last node.
3. Inside the loop, move `temp` to the next node.
4. Check if `temp` is equal to the head node. If it is, the list is already circular, so return.
5. After the loop, update the next pointer of the last node (`temp`) to point to the head node.
6. This completes the conversion of the singly linked list into a circular linked list.

## Time Complexity

The time complexity of converting a singly linked list into a circular linked list is O(n), where n is the number of nodes in the linked list. This is because we traverse the entire linked list once to reach the last node and update its next pointer.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post