Convert singly linked list into circular linked list
Given a singly linked list, the task is to convert it into a circular linked list.
Examples:
Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
Output:
Explanation: Singly linked list is converted to circular by pointing last node to head.
Input: head: 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> NULL
Output:
Explanation: Singly linked list is converted to circular by pointing last node to head.
Table of Content
[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:
// C++ Program for converting singly linked list
// into circular linked list.
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function that convert singly linked list
// into circular linked list.
void circular(Node *curr, Node *head) {
// if last node, then point next ptr
// to head Node
if (curr->next == nullptr) {
curr->next = head;
return;
}
// otherwise traverse to the
// next node
circular(curr->next, head);
}
void printList(Node *head) {
Node *curr = head;
do {
cout << curr->data << " ";
curr = curr->next;
} while (curr != head);
cout << endl;
}
int main() {
// create a hard coded list
// 10->12->14->16
Node *head = new Node(10);
head->next = new Node(12);
head->next->next = new Node(14);
head->next->next->next = new Node(16);
circular(head, head);
printList(head);
return 0;
}
// C Program for converting singly linked list
// into circular linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function that converts singly linked list into
// circular linked list
void circular(struct Node* curr, struct Node* head) {
// if last node, then point next ptr
// to head Node
if (curr->next == NULL) {
curr->next = head;
return;
}
// otherwise traverse to the
// next node
circular(curr->next, head);
}
void printList(struct Node* head) {
struct Node* curr = head;
do {
printf("%d ", curr->data);
curr = curr->next;
} while (curr != head);
printf("\n");
}
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
// create a hard coded list 10->12->14->16
struct Node* head = createNode(10);
head->next = createNode(12);
head->next->next = createNode(14);
head->next->next->next = createNode(16);
circular(head, head);
printList(head);
return 0;
}
// Java Program for converting singly linked list
// into circular linked list.
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class GfG {
// Function that converts singly linked
// list into circular linked list
static void circular(Node curr, Node head) {
// if last node, then point next ptr
// to head Node
if (curr.next == null) {
curr.next = head;
return;
}
// otherwise move to the
// next node
circular(curr.next, head);
}
static void printList(Node head) {
Node curr = head;
do {
System.out.print(curr.data + " ");
curr = curr.next;
} while (curr != head);
System.out.println();
}
public static void main(String[] args) {
// create a hard coded list 10->12->14->16
Node head = new Node(10);
head.next = new Node(12);
head.next.next = new Node(14);
head.next.next.next = new Node(16);
circular(head, head);
printList(head);
}
}
# Python program for converting singly linked list
# into circular linked list.
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function that converts singly linked list
# into circular linked list
def circular(curr, head):
# if last node, then point next ptr
# to head Node
if curr.next == None:
curr.next = head
return
# otherwise move to the
# next node
circular(curr.next, head)
def printList(head):
curr = head
while True:
print(curr.data, end=" ")
curr = curr.next
if curr == head:
break
print()
if __name__ == "__main__":
# create a hard coded list 10->12->14->16
head = Node(10)
head.next = Node(12)
head.next.next = Node(14)
head.next.next.next = Node(16)
circular(head, head)
printList(head)
// C# Program for converting singly linked list
// into circular linked list.
using System;
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class GfG {
// Function that converts singly linked list
// into circular linked list
static void Circular(Node curr, Node head) {
// if last node, then point next ptr
// to head Node
if (curr.next == null) {
curr.next = head;
return;
}
// otherwise move to the
// next node
Circular(curr.next, head);
}
static void PrintList(Node head) {
Node curr = head;
do {
Console.Write(curr.data + " ");
curr = curr.next;
} while (curr != head);
Console.WriteLine();
}
static void Main(string[] args) {
// create a hard coded list 10->12->14->16
Node head = new Node(10);
head.next = new Node(12);
head.next.next = new Node(14);
head.next.next.next = new Node(16);
Circular(head, head);
PrintList(head);
}
}
// Javascript program for converting singly linked list
// into circular linked list.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function that converts singly linked
// list into circular linked list
function circular(curr, head) {
// if last node, then point next ptr
// to head Node
if (curr.next == null) {
curr.next = head;
return;
}
// otherwise move to the
// next node
circular(curr.next, head);
}
function printList(head) {
let curr = head;
do {
console.log(curr.data + " ");
curr = curr.next;
} while (curr !== head);
}
// create a hard coded list
// 10->12->14->16
let head = new Node(10);
head.next = new Node(12);
head.next.next = new Node(14);
head.next.next.next = new Node(16);
circular(head, head);
printList(head);
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:
// C++ Program for converting singly linked list
// into circular linked list.
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function that convert singly linked list
// into circular linked list.
Node *circular(Node *head) {
// declare a node variable curr and
// assign head node into curr node.
Node *curr = head;
// check that while curr->next is not equal
// to NULL then curr points to next node.
while (curr->next != nullptr)
curr = curr->next;
// assign curr->next to the head node.
curr->next = head;
return head;
}
void printList(Node *head) {
Node *curr = head;
do {
cout << curr->data << " ";
curr = curr->next;
} while (curr != head);
cout << endl;
}
int main() {
// create a hard coded list
// 10->12->14->16
Node *head = new Node(10);
head->next = new Node(12);
head->next->next = new Node(14);
head->next->next->next = new Node(16);
head = circular(head);
printList(head);
return 0;
}
// C Program for converting singly linked list
// into circular linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function that converts singly linked list into
// circular linked list
struct Node* circular(struct Node* head) {
// declare a node variable curr and
// assign head node into curr node.
struct Node* curr = head;
// check that while curr->next is not equal
// to NULL then curr points to next node.
while (curr->next != NULL)
curr = curr->next;
// assign curr->next to the head node.
curr->next = head;
return head;
}
void printList(struct Node* head) {
struct Node* curr = head;
do {
printf("%d ", curr->data);
curr = curr->next;
} while (curr != head);
printf("\n");
}
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
// create a hard coded list 10->12->14->16
struct Node* head = createNode(10);
head->next = createNode(12);
head->next->next = createNode(14);
head->next->next->next = createNode(16);
head = circular(head);
printList(head);
return 0;
}
// Java Program for converting singly linked list
// into circular linked list.
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class GfG {
// Function that converts singly linked
// list into circular linked list
static Node circular(Node head) {
// declare a node variable curr and
// assign head node into curr node.
Node curr = head;
// check that while curr->next is not equal
// to NULL then curr points to next node.
while (curr.next != null)
curr = curr.next;
// assign curr->next to the head node.
curr.next = head;
return head;
}
static void printList(Node head) {
Node curr = head;
do {
System.out.print(curr.data + " ");
curr = curr.next;
} while (curr != head);
System.out.println();
}
public static void main(String[] args) {
// create a hard coded list 10->12->14->16
Node head = new Node(10);
head.next = new Node(12);
head.next.next = new Node(14);
head.next.next.next = new Node(16);
head = circular(head);
printList(head);
}
}
# Python program for converting singly linked list
# into circular linked list.
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function that converts singly linked list
# into circular linked list
def circular(head):
# declare a node variable curr and
# assign head node into curr node.
curr = head
# check that while curr->next is not equal
# to NULL then curr points to next node.
while curr.next is not None:
curr = curr.next
# assign curr->next to the head node.
curr.next = head
return head
def printList(head):
curr = head
while True:
print(curr.data, end=" ")
curr = curr.next
if curr == head:
break
print()
if __name__ == "__main__":
# create a hard coded list 10->12->14->16
head = Node(10)
head.next = Node(12)
head.next.next = Node(14)
head.next.next.next = Node(16)
head = circular(head)
printList(head)
// C# Program for converting singly linked list
// into circular linked list.
using System;
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class GfG {
// Function that converts singly linked list
// into circular linked list
static Node Circular(Node head) {
// declare a node variable curr and
// assign head node into curr node.
Node curr = head;
// check that while curr->next is not equal
// to NULL then curr points to next node.
while (curr.next != null)
curr = curr.next;
// assign curr->next to the head node.
curr.next = head;
return head;
}
static void PrintList(Node head) {
Node curr = head;
do {
Console.Write(curr.data + " ");
curr = curr.next;
} while (curr != head);
Console.WriteLine();
}
static void Main(string[] args) {
// create a hard coded list 10->12->14->16
Node head = new Node(10);
head.next = new Node(12);
head.next.next = new Node(14);
head.next.next.next = new Node(16);
head = Circular(head);
PrintList(head);
}
}
// Javascript program for converting singly linked list
// into circular linked list.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function that converts singly linked
// list into circular linked list
function circular(head) {
// declare a node variable curr and
// assign head node into curr node.
let curr = head;
// check that while curr->next is not equal
// to NULL then curr points to next node.
while (curr.next !== null) {
curr = curr.next;
}
// assign curr->next to the head node.
curr.next = head;
return head;
}
function printList(head) {
let curr = head;
do {
console.log(curr.data + " ");
curr = curr.next;
} while (curr !== head);
}
// create a hard coded list
// 10->12->14->16
let head = new Node(10);
head.next = new Node(12);
head.next.next = new Node(14);
head.next.next.next = new Node(16);
head = circular(head);
printList(head);
Output
10 12 14 16
Time Complexity: O(n), where n is the number of nodes in the list.
Auxiliary Space: O(1)