Insertion at the beginning in circular linked list (original) (raw)
Last Updated : 30 Aug, 2025
Given a Circular Linked List and an integer key representing a node. Insert the given key node at the beginning of the circular linked list.
**Input: key = 5
**Output: 5 -> 1 -> 7 -> 8 -> 10
**Explanation: After inserting 5 at the beginning of the given circular linked list, it has elements as 5, 1, 7, 8, 10.
**Input: key = 1
**Output: 1 -> 2 -> 5 -> 7 -> 8 -> 10
**Explanation: After inserting 1 at the beginning of the given circular linked list, it has elements as 1, 2, 5, 7, 8, 10.
Insertion at the beginning in circular linked list
To insert a new node at the beginning of a circular linked list, we first create the **new node and allocate memory for it. If the list is empty (indicated by the last pointer being **NULL), we make the **new node point to itself. If the list already contains nodes then we set the **new node’s next pointer to point to the **current head of the list (which is **last->next), and then update the last node’s next pointer to point to the **new node. This maintains the circular structure of the list.
Step-by-step approach:
- Create a new node with the given value.
- Check Empty List (**last == **nullptr):
- Make **newNode->next point to itself.
- Insert at Beginning:
- Set **newNode->next to **last->next.
- Update last->next to **newNode.
Below is the implementation of the above approach:
C++ `
#include using namespace std;
class Node { public: int data; Node* next;
Node(int x) {
data = x;
next = nullptr;
}};
Node* insertAtBeginning(Node* last, int key) { Node* newNode = new Node(key);
if (last == nullptr) {
newNode->next = newNode;
return newNode;
}
newNode->next = last->next;
last->next = newNode;
return last;}
void printList(Node* last) { if (last == nullptr) return;
Node* head = last->next;
Node* temp = head;
do {
cout << temp->data;
temp = temp->next;
if (temp != head) cout << " -> ";
} while (temp != head);
cout << endl;}
int main() { // Create circular linked list: 2 -> 3 -> 4 Node* first = new Node(2); first->next = new Node(3); first->next->next = new Node(4);
Node* last = first->next->next;
last->next = first;
// Insert 5 at the beginning
last = insertAtBeginning(last, 5);
printList(last);
return 0;}
Java
class Node { int data; Node next;
Node(int x) {
data = x;
next = null;
}}
class GfG {
static Node insertAtBeginning(Node last, int key) {
Node newNode = new Node(key);
if (last == null) {
newNode.next = newNode;
return newNode;
}
newNode.next = last.next;
last.next = newNode;
return last;
}
static void printList(Node last) {
if (last == null) return;
Node head = last.next;
Node temp = head;
do {
System.out.print(temp.data);
temp = temp.next;
if (temp != head) System.out.print(" -> ");
} while (temp != head);
System.out.println();
}
public static void main(String[] args) {
// Create circular linked list: 2 -> 3 -> 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
// Insert 5 at the beginning
last = insertAtBeginning(last, 5);
printList(last);
}}
Python
class Node: def init(self, x): self.data = x self.next = None
def insertAtBeginning(last, key): newNode = Node(key)
if last is None:
newNode.next = newNode
return newNode
newNode.next = last.next
last.next = newNode
return lastdef printList(last): if last is None: return
head = last.next
temp = head
while True:
print(temp.data, end="")
temp = temp.next
if temp != head:
print(" -> ", end="")
else:
break
print()if name == "main": # Create circular linked list: 2 -> 3 -> 4 first = Node(2) first.next = Node(3) first.next.next = Node(4)
last = first.next.next
last.next = first
# Insert 5 at the beginning
last = insertAtBeginning(last, 5)
printList(last)C#
using System;
class Node { public int data; public Node next;
public Node(int x) {
data = x;
next = null;
}}
class GfG { public static Node insertAtBeginning(Node last, int key) { Node newNode = new Node(key);
if (last == null) {
newNode.next = newNode;
return newNode;
}
newNode.next = last.next;
last.next = newNode;
return last;
}
public static void printList(Node last) {
if (last == null) return;
Node head = last.next;
Node temp = head;
while (true) {
Console.Write(temp.data);
temp = temp.next;
if (temp != head) {
Console.Write(" -> ");
} else {
break;
}
}
Console.WriteLine();
}
static void Main(string[] args) {
// Create circular linked list: 2 -> 3 -> 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
// Insert 5 at the beginning
last = insertAtBeginning(last, 5);
printList(last);
}}
JavaScript
class Node { constructor(x) { this.data = x; this.next = null; } }
// Function to insert at beginning of circular linked list function insertAtBeginning(last, key) { let newNode = new Node(key);
if (last === null) {
newNode.next = newNode;
return newNode;
}
newNode.next = last.next;
last.next = newNode;
return last;}
// Function to print circular linked list function printList(last) { if (last === null) return;
let head = last.next;
let temp = head;
while (true) {
process.stdout.write(temp.data.toString());
temp = temp.next;
if (temp !== head) {
process.stdout.write(" -> ");
} else {
break;
}
}
console.log();}
// Driver code let first = new Node(2); first.next = new Node(3); first.next.next = new Node(4);
let last = first.next.next; last.next = first;
// Insert 5 at beginning last = insertAtBeginning(last, 5);
printList(last);
`
**Time Complexity: O(1)
**Auxiliary Space: O(1)



