Insertion at the End in Circular Linked List - O(1) Time and O(1) Space
The idea is to create a new node and insert it immediately after the current tail of the circular linked list. If the list is empty, make the new node point to itself and return it as the tail. Otherwise, link the new node to the current head (tail->next), update the current tail's next pointer to the new node, and return the new node as the updated tail. This inserts the node at the end while maintaining the circular nature of the linked list.
Step-by-step approach:
Create a new node with the given key.
If the circular linked list is empty (tail == nullptr), make newNode->next point to itself and return newNode as the tail.
Otherwise, set newNode->next to tail->next (the current head).
Update tail->next to newNode.
Return newNode as the new tail.
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};Node*insertAtEnd(Node*tail,intkey){// Create a new node with the given key.Node*newNode=newNode(key);// If the circular linked list is empty,// make the new node point to itself.if(tail==nullptr){newNode->next=newNode;returnnewNode;}// Insert the new node after the tail// and before the current head.newNode->next=tail->next;tail->next=newNode;// The newly inserted node becomes the new tail.returnnewNode;}// Function to print the circular linked listvoidprintList(Node*tail){if(tail==nullptr){cout<<"Empty";return;}Node*curr=tail->next;do{cout<<curr->data;curr=curr->next;if(curr!=tail->next)cout<<"->";}while(curr!=tail->next);}intmain(){// Create the circular linked list: 2 -> 5 -> 7 -> 8 -> 10Node*tail=newNode(10);tail->next=newNode(2);tail->next->next=newNode(5);tail->next->next->next=newNode(7);tail->next->next->next->next=newNode(8);tail->next->next->next->next->next=tail;intkey=1;tail=insertAtEnd(tail,key);printList(tail);return0;}
Java
importjava.util.*;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticNodeinsertAtEnd(Nodetail,intkey){// Create a new node with the given key.NodenewNode=newNode(key);// If the circular linked list is empty,// make the new node point to itself.if(tail==null){newNode.next=newNode;returnnewNode;}// Insert the new node after the tail// and before the current head.newNode.next=tail.next;tail.next=newNode;// The newly inserted node becomes the new tail.returnnewNode;}// Function to print the circular linked listpublicstaticvoidprintList(Nodetail){if(tail==null){System.out.print("Empty");return;}Nodecurr=tail.next;do{System.out.print(curr.data);curr=curr.next;if(curr!=tail.next)System.out.print("->");}while(curr!=tail.next);}publicstaticvoidmain(String[]args){// Create the circular linked list: 2 -> 5 -> 7 -> 8// -> 10Nodetail=newNode(10);tail.next=newNode(2);tail.next.next=newNode(5);tail.next.next.next=newNode(7);tail.next.next.next.next=newNode(8);tail.next.next.next.next.next=tail;intkey=1;tail=insertAtEnd(tail,key);printList(tail);}}
Python
classNode:def__init__(self,data):self.data=dataself.next=NonedefinsertAtEnd(tail,key):# Create a new node with the given key.newNode=Node(key)# If the circular linked list is empty,# make the new node point to itself.iftailisNone:newNode.next=newNodereturnnewNode# Insert the new node after the tail# and before the current head.newNode.next=tail.nexttail.next=newNode# The newly inserted node becomes the new tail.returnnewNode# Function to print the circular linked listdefprintList(tail):iftailisNone:print('Empty')returncurrent=tail.nextwhileTrue:print(current.data,end='')current=current.nextifcurrent!=tail.next:print('->',end='')ifcurrent==tail.next:break# Create the circular linked list: 2 -> 5 -> 7 -> 8 -> 10tail=Node(10)tail.next=Node(2)tail.next.next=Node(5)tail.next.next.next=Node(7)tail.next.next.next.next=Node(8)tail.next.next.next.next.next=tailkey=1tail=insertAtEnd(tail,key)printList(tail)
C#
usingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticNodeinsertAtEnd(Nodetail,intkey){// Create a new node with the given key.NodenewNode=newNode(key);// If the circular linked list is empty,// make the new node point to itself.if(tail==null){newNode.next=newNode;returnnewNode;}// Insert the new node after the tail// and before the current head.newNode.next=tail.next;tail.next=newNode;// The newly inserted node becomes the new tail.returnnewNode;}// Function to print the circular linked listpublicstaticvoidprintList(Nodetail){if(tail==null){Console.Write("Empty");return;}Nodecurr=tail.next;do{Console.Write(curr.data);curr=curr.next;if(curr!=tail.next)Console.Write("->");}while(curr!=tail.next);}publicstaticvoidMain(){// Create the circular linked list: 2 -> 5 -> 7 -> 8// -> 10Nodetail=newNode(10);tail.next=newNode(2);tail.next.next=newNode(5);tail.next.next.next=newNode(7);tail.next.next.next.next=newNode(8);tail.next.next.next.next.next=tail;intkey=1;tail=insertAtEnd(tail,key);printList(tail);}}
JavaScript
classNode{constructor(data){this.data=data;this.next=null;}}functioninsertAtEnd(tail,key){// Create a new node with the given key.constnewNode=newNode(key);// If the circular linked list is empty,// make the new node point to itself.if(tail===null){newNode.next=newNode;returnnewNode;}// Insert the new node after the tail// and before the current head.newNode.next=tail.next;tail.next=newNode;// The newly inserted node becomes the new tail.returnnewNode;}functionprintList(tail){if(tail===null){console.log("Empty");return;}letcurr=tail.next;do{process.stdout.write(curr.data.toString());curr=curr.next;if(curr!==tail.next){process.stdout.write("->");}}while(curr!==tail.next);}// Driver Code// Create the circular linked list: 2 -> 5 -> 7 -> 8 -> 10lettail=newNode(10);tail.next=newNode(2);tail.next.next=newNode(5);tail.next.next.next=newNode(7);tail.next.next.next.next=newNode(8);tail.next.next.next.next.next=tail;constkey=1;tail=insertAtEnd(tail,key);printList(tail);