Insertion at the beginning in circular linked list
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<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};Node*insertAtBeginning(Node*last,intkey){Node*newNode=newNode(key);if(last==nullptr){newNode->next=newNode;returnnewNode;}newNode->next=last->next;last->next=newNode;returnlast;}voidprintList(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;}intmain(){// Create circular linked list: 2 -> 3 -> 4Node*first=newNode(2);first->next=newNode(3);first->next->next=newNode(4);Node*last=first->next->next;last->next=first;// Insert 5 at the beginninglast=insertAtBeginning(last,5);printList(last);return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGfG{staticNodeinsertAtBeginning(Nodelast,intkey){NodenewNode=newNode(key);if(last==null){newNode.next=newNode;returnnewNode;}newNode.next=last.next;last.next=newNode;returnlast;}staticvoidprintList(Nodelast){if(last==null)return;Nodehead=last.next;Nodetemp=head;do{System.out.print(temp.data);temp=temp.next;if(temp!=head)System.out.print(" -> ");}while(temp!=head);System.out.println();}publicstaticvoidmain(String[]args){// Create circular linked list: 2 -> 3 -> 4Nodefirst=newNode(2);first.next=newNode(3);first.next.next=newNode(4);Nodelast=first.next.next;last.next=first;// Insert 5 at the beginninglast=insertAtBeginning(last,5);printList(last);}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefinsertAtBeginning(last,key):newNode=Node(key)iflastisNone:newNode.next=newNodereturnnewNodenewNode.next=last.nextlast.next=newNodereturnlastdefprintList(last):iflastisNone:returnhead=last.nexttemp=headwhileTrue:print(temp.data,end="")temp=temp.nextiftemp!=head:print(" -> ",end="")else:breakprint()if__name__=="__main__":# Create circular linked list: 2 -> 3 -> 4first=Node(2)first.next=Node(3)first.next.next=Node(4)last=first.next.nextlast.next=first# Insert 5 at the beginninglast=insertAtBeginning(last,5)printList(last)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGfG{publicstaticNodeinsertAtBeginning(Nodelast,intkey){NodenewNode=newNode(key);if(last==null){newNode.next=newNode;returnnewNode;}newNode.next=last.next;last.next=newNode;returnlast;}publicstaticvoidprintList(Nodelast){if(last==null)return;Nodehead=last.next;Nodetemp=head;while(true){Console.Write(temp.data);temp=temp.next;if(temp!=head){Console.Write(" -> ");}else{break;}}Console.WriteLine();}staticvoidMain(string[]args){// Create circular linked list: 2 -> 3 -> 4Nodefirst=newNode(2);first.next=newNode(3);first.next.next=newNode(4);Nodelast=first.next.next;last.next=first;// Insert 5 at the beginninglast=insertAtBeginning(last,5);printList(last);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}// Function to insert at beginning of circular linked listfunctioninsertAtBeginning(last,key){letnewNode=newNode(key);if(last===null){newNode.next=newNode;returnnewNode;}newNode.next=last.next;last.next=newNode;returnlast;}// Function to print circular linked listfunctionprintList(last){if(last===null)return;lethead=last.next;lettemp=head;while(true){process.stdout.write(temp.data.toString());temp=temp.next;if(temp!==head){process.stdout.write(" -> ");}else{break;}}console.log();}// Driver codeletfirst=newNode(2);first.next=newNode(3);first.next.next=newNode(4);letlast=first.next.next;last.next=first;// Insert 5 at beginninglast=insertAtBeginning(last,5);printList(last);