Given a singly linked list, we have to convert it into circular linked list. For example, we have been given a singly linked list with four nodes and we want to convert this singly linked list into circular linked list.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to traverse the singly linked list and check if the node is the last node or not. If the node is the last node i.e pointing to NULL then make it point to the starting node i.e head node. Below is the implementation of this approach.

```// Program for converting singly linked list
#include <bits/stdc++.h>

struct Node {
int data;
struct Node* next;
};

// Function that convert singly linked list
{
// declare a node variable start and
// assign head node into start node.

// check that while head->next not equal
// to NULL then head points to next node.

// if head->next points to NULL then
// start assign to the head->next node.
return start;
}

void push(struct Node** head, int data)
{
// Allocate dynamic memory for newNode.
struct Node* newNode = (struct Node*)malloc
(sizeof(struct Node));

// Assign the data into newNode.
newNode->data = data;

// newNode->next assign the address of

}

// Function that display the elements of
void displayList(struct Node* node)
{
struct Node* start = node;

while (node->next != start) {
printf("%d ", node->data);
node = node->next;
}

// Display the last node of circular
printf("%d ", node->data);
}

// Drier program to test the functions
int main()
{

// Using push() function to construct
// 17->22->13->14->15

// Call the circular_list function that
// convert singly linked list to circular

printf("Display list: \n");

return 0;
}
```

Output:

```Display list:
17 22 13 14 15
```

This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GATE CS Corner    Company Wise Coding Practice

1.5 Average Difficulty : 1.5/5.0
Based on 2 vote(s)