Convert singly linked list into circular linked list
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():
if head is NULL:
print "Empty Linked List"
else:
temp = head
while temp.next is not NULL:
temp = temp.next
if temp is head:
// The list is already circular
return
// Connect the last node to the head node
temp.next = head
Algorithm Explanation
 Check if the linked list is empty. If it's empty, print "Empty Linked List."
 Initialize
temp
to the head node and use a loop to traverse the linked list until the last node.  Inside the loop, move
temp
to the next node.  Check if
temp
is equal to the head node. If it is, the list is already circular, so return.  After the loop, update the next pointer of the last node (
temp
) to point to the head node.  This completes the conversion of the singly linked list into a circular linked list.
Code Solution

1) Convert singly linked list to circular list in java
2) Convert singly linked list to circular list in c++
3) Convert singly linked list to circular list in c
4) Convert singly linked list to circular list in c#
5) Convert singly linked list to circular list in php
6) Convert singly linked list to circular list in python
7) Convert singly linked list to circular list in ruby
8) Convert singly linked list to circular list in scala
9) Convert singly linked list to circular list in swift
10) Convert singly linked list to circular list in kotlin
11) Convert singly linked list to circular list in golang
12) Convert singly linked list to circular list in vb.net
13) Convert singly linked list to circular list in node js
14) Convert singly linked list to circular list in typescript
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.
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.
New Comment