Linked List January 28 ,2026

Singly to Circular Linked List Conversion

Problem Statement

Given a singly linked list, convert it into a circular linked list by modifying the next pointer of the last node so that it points to the head node.

In a singly linked list, the last node points to NULL. After conversion, the last node should point back to the first node, forming a circular structure.

Example

Input

Singly Linked List:
10 → 20 → 30 → 40 → NULL

Output

Circular Linked List:
10 → 20 → 30 → 40 → back to 10

Approach Explanation

To convert a singly linked list into a circular linked list:

  • Traverse the list to find the last node
  • Update the next pointer of the last node to point to the head
  • This maintains all existing nodes and simply modifies one pointer

This approach is simple, efficient, and does not require extra memory.

Algorithm

  1. If head == NULL, return (empty list cannot be converted).
  2. Initialize a pointer current = head.
  3. Traverse the list until current->next == NULL.
  4. Set current->next = head.
  5. The singly linked list is now converted into a circular linked list.

Time Complexity

  • Best Case: O(1) (single node list)
  • Worst Case: O(n)
  • Average Case: O(n)

Space Complexity

  • O(1)
    No additional memory is used.

C Implementation

// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h"

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

// Function to convert singly linked list to circular
void convertToCircular(struct Node* head) {
    if (head == NULL)
        return;

    struct Node* current = head;

    while (current->next != NULL) {
        current = current->next;
    }

    current->next = head;
}

// Function to create a new node
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Function to print circular linked list
void printCircular(struct Node* head) {
    if (head == NULL)
        return;

    struct Node* temp = head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);

    printf("(back to head)\n");
}

int main() {
    // Creating linked list: 1 -> 2 -> 3 -> 4
    struct Node* head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);
    head->next->next->next = createNode(4);

    // Convert to circular
    convertToCircular(head);

    // Print circular list
    printCircular(head);

    return 0;
}

C++ Implementation

// While running this code, replace all #include "..." with #include <...>

#include "iostream"
using namespace std;

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

// Convert singly linked list to circular
void convertToCircular(Node* head) {
    if (head == NULL)
        return;

    Node* current = head;

    while (current->next != NULL) {
        current = current->next;
    }

    current->next = head;
}

// Create new node
Node* createNode(int data) {
    Node* newNode = new Node();
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Print circular linked list
void printCircular(Node* head) {
    if (head == NULL)
        return;

    Node* temp = head;

    do {
        cout << temp->data << " -> ";
        temp = temp->next;
    } while (temp != head);

    cout << "(back to head)" << endl;
}

int main() {
    Node* head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);
    head->next->next->next = createNode(4);
    head->next->next->next->next = createNode(5);

    convertToCircular(head);
    printCircular(head);

    return 0;
}

Java Implementation

static void convertToCircular(Node head) {
    if (head == null)
        return;

    Node current = head;
    while (current.next != null) {
        current = current.next;
    }

    current.next = head;
}

Python Implementation

def convert_to_circular(head):
    if head is None:
        return

    current = head
    while current.next is not None:
        current = current.next

    current.next = head

JavaScript Implementation

function convertToCircular(head) {
    if (head === null)
        return;

    let current = head;
    while (current.next !== null) {
        current = current.next;
    }

    current.next = head;
}

Output-

1 -> 2 -> 3 -> 4 -> (back to head)

Edge Cases to Consider

  • Empty linked list
  • Single node list
  • Already circular linked list (should be checked if required)

Advantages of Conversion

  • Enables circular traversal
  • Useful for round-robin scheduling
  • Efficient memory usage
  • Simplifies repeated traversal logic

Key Points to Remember

  • Only the last node’s next pointer is modified
  • No new nodes are created
  • Circularity must be maintained carefully

Conclusion

Converting a singly linked list into a circular linked list is a straightforward operation that involves traversing to the last node and linking it back to the head. This operation runs in linear time with constant space and is commonly used in scheduling, buffering, and cyclic data structures.


Next Problem in the Series

 Exchange First and Last Nodes in a Circular Linked List

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Remove Eve...
Linked List January 01 ,2026

Remove Every K-th No...

Middle of...
Linked List January 01 ,2026

Middle of a Linked L...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Circular L...
Linked List January 01 ,2026

Circular Linked List...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Print a Si...
Linked List February 02 ,2026

Print a Singly Linke...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Deleting a...
Linked List February 02 ,2026

Deleting a Node at a...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Delete Fir...
Linked List February 02 ,2026

Delete First Node of...

Check if L...
Linked List February 02 ,2026

Check if Linked List...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Swap Nodes...
Linked List February 02 ,2026

Swap Nodes in Pairs

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com