# Insert node at beginning of circular linked list

Circular linked list, last node next pointer are not NULL. That are contain the reference of first node of linked list. That is difference of single and circular linked list. Basically we can choose two variant to make a circular single linked list. First only one head pointer. And another is two pointers head and tail.

First Approach : That is very simple and similar to single linked list. And inserting a new node to beginning of this linked list this are need O(1) time. But the properties of circular linked list, always last node next pointer hold the address of first node of linked list.

So finding last node and provide next pointer reference to new head this process are tack O(n) time. So overall time complexity of first approach is O(n).

For example, suppose we are inserted the following (1,2,3,4,5,6,7,8) node in a sequence.

Here given code implementation process.

``````//C Program
#include <stdio.h>
//for malloc function
#include <stdlib.h>

//Create structure
struct Node
{
int data;
struct Node*next;
};

//insert Node at beginning of linked list
{
//Create dynamic node
struct Node*node = (struct Node*)malloc(sizeof(struct Node));
if(node==NULL)
{
printf("Memory overflow\n");
}
else
{
node->data=value;

{
node->next=node;
}
else
{

//Find last node
{
temp=temp->next;
}
//connect last node next pointer is to new node
temp->next=node;
}

}
}
{

{
}
else
{
while(temp)
{
printf("%d  ",temp->data);
temp=temp->next;
{
break; //terminate loop
}
}

}
}
int main()
{

//Create node pointer
//display all node
return 0;
}```
```

#### Output

``Linked List Element : 1  2  3  4  5  6  7  8``
``````//C++ Program
//Insert circular linked list at beginning position
//Define class of linked list Node
#include<iostream>

using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data, Node *next) {
this->data = data;
this->next = next;
}
};
public:
//Class constructors
}
//Insert node at begining of linked list
void insert(int value) {
//Create a node
Node *node = new Node(value, this->head);
} else {
//Find last node
temp = temp->next;
}
temp->next = node;
}
}
//Display node element of circular linked list
void display() {
} else {
cout << "Circular Linked List Element :";
while (temp != NULL) {
cout << " " << temp->data;
temp = temp->next;
break;
}
}
}
}
};
int main() {
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
return 0;
}```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````//Java Program
//Insert circular linked list at beginning position

//Define class of linked list Node
class Node
{
public int data;
public Node next;
public Node(int data, Node next)
{
this.data = data;
this.next = next;
}
}
{

//Class constructors
{
}
//Insert node at begining of linked list
public void insert(int value)
{
//Create a node

{
}
else
{
//Find last node
{
temp=temp.next;
}
temp.next=node;
}

}
//Display node element of circular linked list
public void display()
{
{
}
else
{

while(temp!=null)
{
System.out.print("  "+temp.data);

temp=temp.next;

{
break;
}
}
}
}

public static void main(String[] args)
{
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);

obj.display();

}
}```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````//C# Program
//Insert circular linked list at beginning position

//Define class of linked list Node
using System;
public class Node {
public int data;
public Node next;
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
//Class constructors
}
//Insert node at begining of linked list
public void insert(int value) {
//Create a node
Node node = new Node(value, head);
} else {
//Find last node
temp = temp.next;
}
temp.next = node;
}
}
//Display node element of circular linked list
public void display() {
} else {
while (temp != null) {
Console.Write(" " + temp.data);
temp = temp.next;
break;;
}
}
}
}
public static void Main(String[] args) {
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
}
}```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````<?php
//Php Program
//Insert circular linked list at beginning position
//Define class of linked list Node
class Node {
public \$data;
public \$next;

function __construct(\$data, \$next) {
\$this->data = \$data;
\$this->next = \$next;
}
}
//Class constructors

function __construct() {
}
//Insert node at begining of linked list
public  function insert(\$value) {
//Create a node
} else {
//Find last node
\$temp = \$temp->next;
}
\$temp->next = \$node;
}
}
//Display node element of circular linked list
public  function display() {
} else {
while (\$temp != null) {
echo(" ". \$temp->data);
\$temp = \$temp->next;
break;
}
}
}
}
}

function main() {
\$obj->insert(8);
\$obj->insert(7);
\$obj->insert(6);
\$obj->insert(5);
\$obj->insert(4);
\$obj->insert(3);
\$obj->insert(2);
\$obj->insert(1);
\$obj->display();

}
main();```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````//Node Js Program
//Insert circular linked list at beginning position
//Define class of linked list Node
class Node {
constructor(data, next) {
this.data = data;
this.next = next;
}
}
//Class constructors
constructor() {
}

//Insert node at begining of linked list
insert(value) {
//Create a node
var node = new Node(value, this.head);
} else {
//Find last node
temp = temp.next;
}

temp.next = node;
}
}

//Display node element of circular linked list
display() {
} else {
while (temp != null) {
process.stdout.write(" " + temp.data);
temp = temp.next;
break;
}
}
}
}
}

function main(args) {
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
}

main();```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````# Python 3 Program
# Insert circular linked list at beginning position

# Define class of linked list Node
class Node :

def __init__(self, data, next) :
self.data = data
self.next = next

# Class constructors
def __init__(self) :

# Insert node at begining of linked list
def insert(self, value) :
# Create a node
# First node of linked list
else :
# Find last node
temp = temp.next

temp.next = node

# Display node element of circular linked list
def display(self) :
print("Empty Linked List", end = "")
else :
print("Circular Linked List Element :", end = "")
while (temp != None) :
print(" ", temp.data, end = "")
temp = temp.next
break

def main() :
# insert following linked list nodes
obj.insert(8)
obj.insert(7)
obj.insert(6)
obj.insert(5)
obj.insert(4)
obj.insert(3)
obj.insert(2)
obj.insert(1)
obj.display()

if __name__ == "__main__":
main()```
```

#### Output

``Circular Linked List Element :  1  2  3  4  5  6  7  8``
``````# Ruby Program
# Insert circular linked list at beginning position

# Define class of linked list Node
class Node
# Define the accessor and reader of class Node
attr_accessor :data, :next_node

def initialize(data, next_node)
self.data = data
self.next_node = next_node
end
end

# Class constructors
def initialize()
end
# Insert node at begining of linked list
def insert(value)
# Create a node
# First node of linked list
else
# Find last node
temp = temp.next_node
end
temp.next_node = node
end
end
# Display node element of circular linked list
def display()
else
while (temp != nil)
print(" ", temp.data)
temp = temp.next_node
break
end
end
end
end
end
def main()
# insert following linked list nodes
obj.insert(8)
obj.insert(7)
obj.insert(6)
obj.insert(5)
obj.insert(4)
obj.insert(3)
obj.insert(2)
obj.insert(1)
obj.display()
end
main()```
```

#### Output

``Circular Linked List Element  : 1 2 3 4 5 6 7 8``
``````//Scala Program
//Insert circular linked list at beginning position

import scala.util.control.Breaks._

//Define class of linked list Node
class Node(var data: Int,
var next: Node) {
}

//Class constructors
def this() {
this(null);
}
//Insert node at begining of linked list
def insert(value: Int): Unit = {
//Create a node
var node: Node = new Node(value, head);

} else {

//Find last node
temp = temp.next;
}
temp.next = node;

}
}
//Display node element of circular linked list
def display(): Unit = {
} else {
breakable
{
while (temp != null) {
print(" " + temp.data);
temp = temp.next;

break;
}
}
}
}
}
}
object Main {
def main(args: Array[String]): Unit = {

obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
}
}```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````//Swift Program
//Insert circular linked list at beginning position

//Define class of linked list Node
class Node {
var data: Int;
var next: Node? ;
init(_ data: Int, _ next: Node? ) {
self.data = data;
self.next = next;
}
}
//Class constructors

init() {
}
//Insert node at begining of linked list
func insert(_ value: Int) {
//Create a node
let node: Node? = Node(value, self.head);
} else {
//Find last node
temp = temp!.next;
}
temp!.next = node;
}
}
//Display node element of circular linked list
func display() {
} else {
print("Circular Linked List Element :", terminator: "");
while (temp != nil) {
print(" ", temp!.data, terminator: "");
temp = temp!.next;
break;
}
}
}
}
}
func main() {
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
}
main();```
```

#### Output

``Circular Linked List Element :  1  2  3  4  5  6  7  8``

Second approach: Using of two pointers head and tail to hold the references of first and last linked list node. this process are no need to find last node so time complexity of this approach is O(1).

``````//C Program

#include <stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node
{
int data;
struct Node*next;
};

//insert Node element
void insert(int value)
{
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL)
{
printf("Memory overflow\n");
}
else
{
node->data=value;
if(tail==NULL)
{
tail=node;
}
//point last node next reference pointer to first node
}
}
void display()
{

{
}
else
{
while(temp)
{
printf("%d  ",temp->data);
temp=temp->next;
{
break; //terminate loop
}
}

}
}
int main()
{

insert(8);
insert(7);
insert(6);
insert(5);
insert(4);
insert(3);
insert(2);
insert(1);
//display all node
display();
return 0;
}```
```

#### Output

``1  2  3  4  5  6  7  8``
``````//C++ Program
//Insert circular linked list at beginning position

#include<iostream>
using namespace std;

//Define class of linked list Node
class Node {
public:
int data;
Node *next;
Node(int data, Node *next) {
this->data = data;
this->next = next;
}
};
public:
Node *tail;
//Class constructors
this->tail = NULL;
}
//insert node at beginning of linke list
void insert(int value) {
//Create a node
Node *node = new Node(value, this->head);
if (this->tail == NULL) {
this->tail = node;
}
}
//Display node element of circular linked list
void display() {
} else {
cout << "Circular Linked List Element :";
while (temp != NULL) {
cout << " " << temp->data;
temp = temp->next;
break;
}
}
}
}
};
int main() {
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
return 0;
}```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````//Java Program
//Insert circular linked list at beginning position

//Define class of linked list Node
class Node
{
public int data;
public Node next;
public Node(int data, Node next)
{
this.data = data;
this.next = next;
}
}
{

public Node tail;

//Class constructors
{
tail = null;
}
//insert node at beginning of linke list
public void insert(int value)
{
//Create a node

if(tail==null)
{
tail=node;
}

}
//Display node element of circular linked list
public void display()
{
{
}
else
{

while(temp!=null)
{
System.out.print("  "+temp.data);

temp=temp.next;

{
break;
}
}
}
}

public static void main(String[] args)
{
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);

obj.display();

}
}```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````//C# Program
//Insert circular linked list at beginning position

using System;
//Define class of linked list Node
public class Node {
public int data;
public Node next;
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
public Node tail;
//Class constructors
tail = null;
}
//insert node at beginning of linke list
public void insert(int value) {
//Create a node
Node node = new Node(value, head);
if (tail == null) {
tail = node;
}
}
//Display node element of circular linked list
public void display() {
} else {
while (temp != null) {
Console.Write(" " + temp.data);
temp = temp.next;
break;;
}
}
}
}
public static void Main(String[] args) {
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
}
}```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````<?php
//Php Program
//Insert circular linked list at beginning position

//Define class of linked list Node
class Node {
public \$data;
public \$next;

function __construct(\$data, \$next) {
\$this->data = \$data;
\$this->next = \$next;
}
}
public \$tail;
//Class constructors

function __construct() {
\$this->tail = null;
}
//insert node at beginning of linke list

public  function insert(\$value) {
//Create a node
if (\$this->tail == null) {
\$this->tail = \$node;
}
}
//Display node element of circular linked list

public  function display() {
} else {
while (\$temp != null) {
echo(" ". \$temp->data);
\$temp = \$temp->next;
break;
}
}
}
}
}

function main() {
\$obj->insert(8);
\$obj->insert(7);
\$obj->insert(6);
\$obj->insert(5);
\$obj->insert(4);
\$obj->insert(3);
\$obj->insert(2);
\$obj->insert(1);
\$obj->display();

}
main();```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````//Node Js Program
//Insert circular linked list at beginning position

//Define class of linked list Node
class Node {
constructor(data, next) {
this.data = data;
this.next = next;
}
}
//Class constructors

constructor() {
this.tail = null;
}

//insert node at beginning of linke list
insert(value) {
//Create a node
var node = new Node(value, this.head);
if (this.tail == null) {
this.tail = node;
}
}

//Display node element of circular linked list
display() {
} else {
while (temp != null) {
process.stdout.write(" " + temp.data);
temp = temp.next;
break;
}
}
}
}
}

function main(args) {
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
}

main();```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````# Python 3 Program
# Insert circular linked list at beginning position
# Using head and tail pointer

# Define class of linked list Node
class Node :

def __init__(self, data, next) :
self.data = data
self.next = next

# Class constructors
def __init__(self) :
self.tail = None

# insert node at beginning of linke list
def insert(self, value) :
# Create a node
if (self.tail == None) :
self.tail = node

# Display node element of circular linked list
def display(self) :
print("Empty Linked List", end = "")
else :
print("Circular Linked List Element :", end = "")
while (temp != None) :
print(" ", temp.data, end = "")
temp = temp.next
break

def main() :
# insert following linked list nodes
obj.insert(8)
obj.insert(7)
obj.insert(6)
obj.insert(5)
obj.insert(4)
obj.insert(3)
obj.insert(2)
obj.insert(1)
obj.display()

if __name__ == "__main__":
main()```
```

#### Output

``Circular Linked List Element :  1  2  3  4  5  6  7  8``
``````# Ruby Program
# Insert circular linked list at beginning position
# Using head and tail nodes

# Define class of linked list Node
class Node
# Define the accessor and reader of class Node
attr_accessor :data, :next_node

def initialize(data, next_node)
self.data = data
self.next_node = next_node
end
end

# Class constructors

def initialize()
@tail = nil
end
# insert node at beginning of linke list
def insert(value)
# Create a node
if (@tail == nil)
@tail = node
end
end
# Display node element of circular linked list
def display()
else
while (temp != nil)
print(" ", temp.data)
temp = temp.next_node
break
end
end
end
end
end
def main()
# insert following linked list nodes
obj.insert(8)
obj.insert(7)
obj.insert(6)
obj.insert(5)
obj.insert(4)
obj.insert(3)
obj.insert(2)
obj.insert(1)
obj.display()
end
main()```
```

#### Output

``Circular Linked List Element  : 1 2 3 4 5 6 7 8``
``````//Scala Program
//Insert circular linked list at beginning position

import scala.util.control.Breaks._

//Define class of linked list Node
class Node(var data: Int,
var next: Node) {
}
var tail: Node) {

//Class constructors
def this() {
this(null,null);
}
//insert node at beginning of linke list
def insert(value: Int): Unit = {
//Create a node
var node: Node = new Node(value, head);
if (tail == null) {
tail = node;
}
}
//Display node element of circular linked list
def display(): Unit = {
} else {
breakable
{
while (temp != null) {
print(" " + temp.data);
temp = temp.next;

break;
}
}
}
}
}
}
object Main {
def main(args: Array[String]): Unit = {

obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
}
}```
```

#### Output

``Circular Linked List Element : 1 2 3 4 5 6 7 8``
``````//Swift Program
//Insert circular linked list at beginning position

//Define class of linked list Node
class Node {
var data: Int;
var next: Node? ;
init(_ data: Int, _ next: Node? ) {
self.data = data;
self.next = next;
}
}
var tail: Node? ;
//Class constructors

init() {
self.tail = nil;
}
//insert node at beginning of linke list
func insert(_ value: Int) {
//Create a node
let node: Node? = Node(value, self.head);
if (self.tail == nil) {
self.tail = node;
}
}
//Display node element of circular linked list
func display() {
} else {
print("Circular Linked List Element :", terminator: "");
while (temp != nil) {
print(" ", temp!.data, terminator: "");
temp = temp!.next;
break;
}
}
}
}
}
func main() {
obj.insert(8);
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
obj.display();
}
main();```
```

#### Output

``Circular Linked List Element :  1  2  3  4  5  6  7  8``

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.