Posted on by Kalkicode

# Insert node at end of circular linked list

In this post are given an example of how to insert new created nodes of end of circular linked list.

Generally there are two way to solve this problem. method 1 Using of single pointer . And method 2 using of two pointer variables.

Method 1: This method are normally used because we are assume that linked list are contain initial node (assumption to add new node to existing list). So this method are useful. First we are need to find last nodes of linked list. But note that in circular linked list last node, next pointer always hold the reference of first linked list node.

So start iteration from head of linked list and check next upcoming node is not a head. When this condition are satisfied then visit next upcoming node. When condition are fail, That means this is a last node of circular list. And add new node to last position , and assign to this inserted next pointer field to reference of head node of linked list.

Test Cases: There is following test case are useful.

1) In case linked list are empty then create new linked list node and assign this address to head pointer of linked list. But note that Circular linked list last node connect to first node by next pointer. So in this case assign the newly created node address is to next pointer .

2) When linked list are not empty then find last linked list node. This process are requires O(n) time. And add new node to last position connect last node to head node by using of last node next pointer fields.

Time complexity of this method are O(n) (Note that We are reducing this complexity in next method). Suppose we are inserted the following (1,2,3,4,5,6,7,8) node in a sequence.

Here given code implementation process.

``````// Java Program
// Insert node at last of circular linked lists

// Define class of linked list Node
{
public int data;
{
this.data = data;
this.next = first;
}
}
{
// Class constructors
{
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
temp.next = node;
}
}
// Display node element of circular linked list
public void display()
{
{
}
else
{
while (temp != null)
{
// Display node
System.out.print("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
public static void main(String[] args)
{
// Display node
}
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Insert node at last of circular linked lists

// Define class of linked list Node
{
public: int data;
{
this->data = data;
this->next = first;
}
};
{
// Class constructors
{
}
// Insert node at end of circular linked list
void insert(int value)
{
// Create a node
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
temp = temp->next;
}
// Add new node at the last
temp->next = node;
}
}
// Display node element of circular linked list
void display()
{
{
cout << "Empty Linked List" << endl;
}
else
{
cout << "Linked List Element :";
while (temp != NULL)
{
// Display node
cout << "  " << temp->data;
// Visit to next node
temp = temp->next;
{
// Stop iteration
return;
}
}
}
}
};
int main()
{
// Display node
return 0;
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// C Program
// Insert node at last of circular linked lists
#include <stdio.h>
// for malloc function
#include <stdlib.h>

{
int data;
};
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
if (cll == NULL)
{
printf("Memory overflow\n");
}
else
{
}
return cll;
}
// Insert node at end of circular linked list
void insert(struct CircularLinkedList *cll, int value)
{
// Create a node
if (node == NULL)
{
// Overflow
return;
}
node->data = value;
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
temp = temp->next;
}
// Add new node at the last
temp->next = node;

}
}
{
{
}
else
{
while (temp)
{
printf("%d  ", temp->data);
// visit to next node
temp = temp->next;
{
break; //terminate loop
}
}
}
}
int main()
{
// Create node pointer
// insert element of linked list
insert(cll, 1);
insert(cll, 2);
insert(cll, 3);
insert(cll, 4);
insert(cll, 5);
insert(cll, 6);
insert(cll, 7);
insert(cll, 8);
// display all nodes
return 0;
}``````

#### Output

``Linked List Element : 1  2  3  4  5  6  7  8``
``````package main
import "fmt"
// Go Program
// Insert node at last of circular linked lists

// Define class of linked list Node
data int
}
return me
}
}
return me
}
// Insert node at end of circular linked list
// Create a node
// First node of linked list
} else {
// Find last node
// Visit to next node
temp = temp.next
}
// Add new node at the last
temp.next = node
}
}
// Display node element of circular linked list
} else {
for (temp != nil) {
// Display node
fmt.Print("  ", temp.data)
// Visit to next node
temp = temp.next
// Stop iteration
return
}
}
}
}
func main() {
// Display node
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Include namespace system
using System;
// Csharp Program
// Insert node at last of circular linked lists

// Define class of linked list Node
{
public int data;
{
this.data = data;
this.next = first;
}
}
{
// Class constructors
{
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
temp.next = node;
}
}
// Display node element of circular linked list
public void display()
{
{
}
else
{
while (temp != null)
{
// Display node
Console.Write("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
public static void Main(string[] args)
{
// Display node
}
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````<?php
// Php Program
// Insert node at last of circular linked lists

// Define class of linked list Node
{
public \$data;
public \$next;
public  function __construct(\$data, \$first)
{
\$this->data = \$data;
\$this->next = \$first;
}
}

{
// Class constructors
function __construct()
{
}
// Insert node at end of circular linked list
public  function insert(\$value)
{
// Create a node
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
\$temp = \$temp->next;
}
// Add new node at the last
\$temp->next = \$node;
}
}
// Display node element of circular linked list
public  function display()
{
{
}
else
{
while (\$temp != NULL)
{
// Display node
printf("%s", "  ".strval(\$temp->data));
// Visit to next node
\$temp = \$temp->next;
{
// Stop iteration
return;
}
}
}
}
public static
function main(\$args)
{
// Display node
}
}

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Node JS Program
// Insert node at last of circular linked lists

// Define class of linked list Node
{
constructor(data, first)
{
this.data = data;
this.next = first;
}
}
{
// Class constructors
constructor()
{
}
// Insert node at end of circular linked list
insert(value)
{
// Create a node
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
temp.next = node;
}
}
// Display node element of circular linked list
display()
{
{
}
else
{
while (temp != null)
{
// Display node
process.stdout.write("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
}

function main()
{
// Display node
}
main();``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````#  Python 3 Program
#  Insert node at last of circular linked lists

#  Define class of linked list Node
def __init__(self, data, first) :
self.data = data
self.next = first

#  Class constructors
def __init__(self) :

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

#  Add new node at the last
temp.next = node

#  Display node element of circular linked list
def display(self) :
else :
print("Linked List Element :", end = "")
while (temp != None) :
#  Display node
print("  ", temp.data, end = "")
#  Visit to next node
temp = temp.next
#  Stop iteration
return

def main() :
#  Display node

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

#### Output

``Linked List Element :   1   2   3   4   5   6   7   8``
``````#  Ruby Program
#  Insert node at last of circular linked lists

#  Define class of linked list Node
attr_accessor :data, :next
def initialize(data, first)
self.data = data
self.next = first
end

end

#  Class constructors
def initialize()
end

#  Insert node at end of circular linked list
def insert(value)
#  Create a node
#  First node of linked list
else

#  Find last node
#  Visit to next node
temp = temp.next
end

#  Add new node at the last
temp.next = node
end

end

#  Display node element of circular linked list
def display()
else

while (temp != nil)
#  Display node
print("  ", temp.data)
#  Visit to next node
temp = temp.next
#  Stop iteration
return
end

end

end

end

end

def main()
#  Display node
end

main()``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Scala Program
// Insert node at last of circular linked lists

// Define class of linked list Node
{
// Class constructors
def this()
{
this(null);
}
// Insert node at end of circular linked list
def insert(value: Int): Unit = {
// Create a node
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
temp.next = node;
}
}
// Display node element of circular linked list
def display(): Unit = {
{
}
else
{
while (temp != null)
{
// Display node
print("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Display node
}
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Swift 4 Program
// Insert node at last of circular linked lists

// Define class of linked list Node
{
var data: Int;
init(_ data: Int, _ first: LinkNode? )
{
self.data = data;
self.next = first;
}
}
{
// Class constructors
init()
{
}
// Insert node at end of circular linked list
func insert(_ value: Int)
{
// Create a node
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
temp = temp!.next;
}
// Add new node at the last
temp!.next = node;
}
}
// Display node element of circular linked list
func display()
{
{
}
else
{
print("Linked List Element :", terminator: "");
while (temp  != nil)
{
// Display node
print("  ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
{
// Stop iteration
return;
}
}
}
}
}
func main()
{
// Display node
}
main();``````

#### Output

``Linked List Element :   1   2   3   4   5   6   7   8``
``````// Kotlin Program
// Insert node at last of circular linked lists

// Define class of linked list Node
{
var data: Int;
constructor(data: Int, first: LinkNode ? )
{
this.data = data;
this.next = first;
}
}
{
// Class constructors
constructor()
{
}
// Insert node at end of circular linked list
fun insert(value: Int): Unit
{
// Create a node
{
// First node of linked list
}
else
{
// Find last node
{
// Visit to next node
temp = temp?.next;
}
// Add new node at the last
temp?.next = node;
}
}
// Display node element of circular linked list
fun display(): Unit
{
{
}
else
{
while (temp != null)
{
// Display node
print("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
}
fun main(args: Array < String > ): Unit
{
// Display node
}``````

#### Output

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

Method 2 : In this scenario used two pointer head and tail. head are store the reference of first inserted nodes. And tail are store the reference of current insert linked list node.

``````// Java Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
{
public int data;
{
this.data = data;
this.next = first;
}
}
{
// Class constructors
{
this.tail = null;
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
{
// First node of linked list
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
// Display node element of circular linked list
public void display()
{
{
}
else
{
while (temp != null)
{
// Display node
System.out.print("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
public static void main(String[] args)
{
// Display node
}
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Include namespace system
using System;
// Csharp Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
{
public int data;
{
this.data = data;
this.next = first;
}
}
{
// Class constructors
{
this.tail = null;
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
{
// First node of linked list
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
// Display node element of circular linked list
public void display()
{
{
}
else
{
while (temp != null)
{
// Display node
Console.Write("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
public static void Main(string[] args)
{
// Display node
}
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
{
public: int data;
{
this->data = data;
this->next = first;
}
};
{
// Class constructors
{
this->tail = NULL;
}
// Insert node at end of circular linked list
void insert(int value)
{
// Create a node
{
// First node of linked list
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
// Display node element of circular linked list
void display()
{
{
cout << "Empty Linked List" << endl;
}
else
{
cout << "Linked List Element :";
while (temp != NULL)
{
// Display node
cout << "  " << temp->data;
// Visit to next node
temp = temp->next;
{
// Stop iteration
return;
}
}
}
}
};
int main()
{
// Display node
return 0;
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// C Program
// Insert node at last of circular linked lists using tail pointer
#include <stdio.h>
// for malloc function
#include <stdlib.h>

{
int data;
};
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
if (cll == NULL)
{
printf("Memory overflow\n");
}
else
{
cll->tail = NULL;
}
return cll;
}
// Insert node at end of circular linked list
void insert(struct CircularLinkedList *cll, int value)
{
// Create a node
if (node == NULL)
{
// Overflow
return;
}
node->data = value;
{
// First node of linked list
cll->tail = node;
}
else
{
cll->tail->next = node;

// New tail node
cll->tail = node;

}
}
{
{
}
else
{
while (temp)
{
printf("%d  ", temp->data);
// visit to next node
temp = temp->next;
{
break; //terminate loop
}
}
}
}
int main()
{
// Create node pointer
// insert element of linked list
insert(cll, 1);
insert(cll, 2);
insert(cll, 3);
insert(cll, 4);
insert(cll, 5);
insert(cll, 6);
insert(cll, 7);
insert(cll, 8);
// display all nodes
return 0;
}``````

#### Output

``Linked List Element : 1  2  3  4  5  6  7  8``
``````package main
import "fmt"
// Go Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
data int
}
me.data = data
me.next = first
return me
}
}
me.tail = nil
return me
}
// Insert node at end of circular linked list
// Create a node
// First node of linked list
this.tail = node
} else {
this.tail.next = node
this.tail = node
}
}
// Display node element of circular linked list
} else {
for (temp != nil) {
// Display node
fmt.Print(" ", temp.data)
// Visit to next node
temp = temp.next
// Stop iteration
return
}
}
}
}
func main() {
// Display node
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````<?php
// Php Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
{
public \$data;
public \$next;
public  function __construct(\$data, \$first)
{
\$this->data = \$data;
\$this->next = \$first;
}
}
{
public \$tail;
// Class constructors
function __construct()
{
\$this->tail = NULL;
}
// Insert node at end of circular linked list
public  function insert(\$value)
{
// Create a node
{
// First node of linked list
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
// Display node element of circular linked list
public  function display()
{
{
}
else
{
while (\$temp != NULL)
{
// Display node
printf("%s", "  ".strval(\$temp->data));
// Visit to next node
\$temp = \$temp->next;
{
// Stop iteration
return;
}
}
}
}
public static
function main(\$args)
{
// Display node
}
}

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Node JS Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
{
constructor(data, first)
{
this.data = data;
this.next = first;
}
}
{
// Class constructors
constructor()
{
this.tail = null;
}
// Insert node at end of circular linked list
insert(value)
{
// Create a node
{
// First node of linked list
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
// Display node element of circular linked list
display()
{
{
}
else
{
while (temp != null)
{
// Display node
process.stdout.write("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
}

function main()
{
// Display node
}
main();``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````#  Python 3 Program
#  Insert node at last of circular linked lists using tail

#  Define class of linked list Node
def __init__(self, data, first) :
self.data = data
self.next = first

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

#  Insert node at end of circular linked list
def insert(self, value) :
#  Create a node
#  First node of linked list
self.tail = node
else :
self.tail.next = node
self.tail = node

#  Display node element of circular linked list
def display(self) :
else :
print("Linked List Element :", end = "")
while (temp != None) :
#  Display node
print(" ", temp.data, end = "")
#  Visit to next node
temp = temp.next
#  Stop iteration
return

def main() :
#  Display node

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

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````#  Ruby Program
#  Insert node at last of circular linked lists using tail

#  Define class of linked list Node
attr_accessor :data, :next
def initialize(data, first)
self.data = data
self.next = first
end

end

#  Class constructors
def initialize()
self.tail = nil
end

#  Insert node at end of circular linked list
def insert(value)
#  Create a node
#  First node of linked list
self.tail = node
else

self.tail.next = node
self.tail = node
end

end

#  Display node element of circular linked list
def display()
else

while (temp != nil)
#  Display node
print("  ", temp.data)
#  Visit to next node
temp = temp.next
#  Stop iteration
return
end

end

end

end

end

def main()
#  Display node
end

main()``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Scala Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
{
// Class constructors
def this()
{
this(null, null);
}
// Insert node at end of circular linked list
def insert(value: Int): Unit = {
// Create a node
{
// First node of linked list
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
// Display node element of circular linked list
def display(): Unit = {
{
}
else
{
while (temp != null)
{
// Display node
print("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Display node
}
}``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Swift 4 Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
{
var data: Int;
init(_ data: Int, _ first: LinkNode? )
{
self.data = data;
self.next = first;
}
}
{
// Class constructors
init()
{
self.tail = nil;
}
// Insert node at end of circular linked list
func insert(_ value: Int)
{
// Create a node
{
// First node of linked list
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
// Display node element of circular linked list
func display()
{
{
}
else
{
print("Linked List Element :", terminator: "");
while (temp  != nil)
{
// Display node
print(" ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
{
// Stop iteration
return;
}
}
}
}
}
func main()
{
// Display node
}
main();``````

#### Output

``Linked List Element :  1  2  3  4  5  6  7  8``
``````// Kotlin Program
// Insert node at last of circular linked lists using tail

// Define class of linked list Node
{
var data: Int;
constructor(data: Int, first: LinkNode ? )
{
this.data = data;
this.next = first;
}
}
{
// Class constructors
constructor()
{
this.tail = null;
}
// Insert node at end of circular linked list
fun insert(value: Int): Unit
{
// Create a node
{
// First node of linked list
this.tail = node;
}
else
{
this.tail?.next = node;
this.tail = node;
}
}
// Display node element of circular linked list
fun display(): Unit
{
{
}
else
{
while (temp != null)
{
// Display node
print("  " + temp.data);
// Visit to next node
temp = temp.next;
{
// Stop iteration
return;
}
}
}
}
}
fun main(args: Array < String > ): Unit
{
// Display node
}``````

#### Output

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