Deletion at different positions in a Circular Linked List
Last Updated :
13 Apr, 2023
tGiven a Circular Linked List. The task is to write programs to delete nodes from this list present at:
- First position.
- Last Position.
- At any given position i .
Deleting first node from Singly Circular Linked List
Examples:
Input : 99->11->22->33->44->55->66
Output : 11->22->33->44->55->66
Input : 11->22->33->44->55->66
Output : 22->33->44->55->66
Deleting First Node from Circular Linked List
Approach:
- Take two pointers current and previous and traverse the list.
- Keep the pointer current fixed pointing to the first node and move previous until it reaches the last node.
- Once, the pointer previous reaches the last node, do the following:
- previous->next = current-> next
- head = previous -> next;
Implementation: Function to delete first node from singly circular linked list.
C++
// Function to delete First node of
// Circular Linked List
void DeleteFirst(struct Node** head)
{
struct Node *previous = *head, *firstNode = *head;
// check if list doesn't have any node
// if not then return
if (*head == NULL) {
printf("\nList is empty\n");
return;
}
// check if list have single node
// if yes then delete it and return
if (previous->next == previous) {
*head = NULL;
return;
}
// traverse second node to first
while (previous->next != *head) {
previous = previous->next;
}
// now previous is last node and
// first node(firstNode) link address
// put in last node(previous) link
previous->next = firstNode->next;
// make second node as head node
*head = previous->next;
free(firstNode);
return;
}
Java
// Function to delete First node of
// Circular Linked List
static void DeleteFirst(Node head)
{
Node previous = head, firstNode = head;
// Check if list doesn't have any node
// if not then return
if (head == null)
{
System.out.printf("\nList is empty\n");
return;
}
// Check if list have single node
// if yes then delete it and return
if (previous.next == previous)
{
head = null;
return;
}
// Traverse second node to first
while (previous.next != head)
{
previous = previous.next;
}
// Now previous is last node and
// first node(firstNode) link address
// put in last node(previous) link
previous.next = firstNode.next;
// Make second node as head node
head = previous.next;
System.gc();
return;
}
// This code is contributed by aashish1995
Python3
# Function delete First node of
# Circular Linked List
def DeleteFirst(head):
previous = head
next = head
# check list have any node
# if not then return
if (head == None) :
print("\nList is empty")
return None
# check list have single node
# if yes then delete it and return
if (previous.next == previous) :
head = None
return None
# traverse second to first
while (previous.next != head):
previous = previous.next
next = previous.next
# now previous is last node and
# next is first node of list
# first node(next) link address
# put in last node(previous) link
previous.next = next.next
# make second node as head node
head = previous.next
return head
# This code is contributed by shivanisinghss2110
C#
using System;
public class GFG{
static public// Function delete First node of
// Circular Linked List
static Node DeleteFirst(Node head)
{
Node previous = head, next = head;
// check list have any node
// if not then return
if (head == null)
{
Console.Write("\nList is empty\n");
return null;
}
// Check if list have single node
// if yes then delete it and return
if (previous.next == previous)
{
head = null;
return null;
}
// Traverse second node to first
while (previous.next != head)
{
previous = previous.next;
next = previous.next;
}
// Now previous is last node and
// first node(firstNode) link address
// put in last node(previous) link
previous.next = next.next;
// Make second node as head node
head = previous.next;
return head;
}
// This code is contributed by shivanisinghss2110.
c void Main (){
// Code
}
JavaScript
<script>
function DeleteFirst(head)
{
previous = head, firstNode = head;
// Check if list doesn't have any node
// if not then return
if (head == null)
{
document.write("<br>List is empty<br>");
return;
}
// Check if list have single node
// if yes then delete it and return
if (previous.next == previous)
{
head = null;
return;
}
// Traverse second node to first
while (previous.next != head)
{
previous = previous.next;
}
// Now previous is last node and
// first node(firstNode) link address
// put in last node(previous) link
previous.next = firstNode.next;
// Make second node as head node
head = previous.next;
return;
}
// This code is contributed by rag2127
</script>
Time Complexity: O(N), where N is the number of nodes in the Linked List.
Auxiliary Space: O(1)
Deleting the last node of the Circular Linked List
Examples:
Input : 99->11->22->33->44->55->66
Output : 99->11->22->33->44->55
Input : 99->11->22->33->44->55
Output : 99->11->22->33->44
Delete last node from Circular Linked List
Approach:
- Take two pointers current and previous and traverse the list.
- Move both pointers such that next of previous is always pointing to current. Keep moving the pointers current and previous until current reaches the last node and previous is at the second last node.
- Once, the pointer current reaches the last node, do the following:
- previous->next = current-> next
- head = previous -> next;
Function to delete last node from the list:
C++
// Function delete last node of
// Circular Linked List
void DeleteLast(struct Node** head)
{
struct Node *current = *head, *temp = *head, *previous;
// check if list doesn't have any node
// if not then return
if (*head == NULL) {
printf("\nList is empty\n");
return;
}
// check if list have single node
// if yes then delete it and return
if (current->next == current) {
*head = NULL;
return;
}
// move first node to last
// previous
while (current->next != *head) {
previous = current;
current = current->next;
}
previous->next = current->next;
*head = previous->next;
free(current);
return;
}
Java
// Function to delete last node of
// Circular Linked List
static Node DeleteLast(Node head)
{
Node current = head, temp = head, previous = null;
// Check if list doesn't have any node
// if not then return
if (head == null)
{
System.out.printf("\nList is empty\n");
return null;
}
// Check if list have single node
// if yes then delete it and return
if (current.next == current)
{
head = null;
return null;
}
// Move first node to last
// previous
while (current.next != head)
{
previous = current;
current = current.next;
}
previous.next = current.next;
head = previous.next;
return head;
}
// This code is contributed by pratham76
Python3
# Function to delete last node of
# Circular Linked List
def DeleteLast(head):
current = head
temp = head
previous = None
# check if list doesn't have any node
# if not then return
if (head == None):
print("\nList is empty")
return None
# check if list have single node
# if yes then delete it and return
if (current.next == current):
head = None
return None
# move first node to last
# previous
while (current.next != head):
previous = current
current = current.next
previous.next = current.next
head = previous.next
return head
# This code is contributed by rutvik_56
C#
// Function to delete last node of
// Circular Linked List
static Node DeleteLast(Node head)
{
Node current = head, temp = head, previous = null;
// check if list doesn't have any node
// if not then return
if (head == null)
{
Console.Write("\nList is empty\n");
return null;
}
// check if list have single node
// if yes then delete it and return
if (current.next == current)
{
head = null;
return null;
}
// move first node to last
// previous
while (current.next != head)
{
previous = current;
current = current.next;
}
previous.next = current.next;
head = previous.next;
return head;
}
// This code is contributed by shivanisinghss2110
JavaScript
<script>
function DeleteLast(head)
{
let current = head, temp = head, previous = null;
// Check if list doesn't have any node
// if not then return
if (head == null)
{
document.write("<br>List is empty<br>");
return null;
}
// Check if list have single node
// if yes then delete it and return
if (current.next == current)
{
head = null;
return null;
}
// Move first node to last
// previous
while (current.next != head)
{
previous = current;
current = current.next;
}
previous.next = current.next;
head = previous.next;
return head;
}
// This code is contributed by avanitrachhadiya2155
</script>
Time Complexity: O(N) where N is the number of nodes in given Linked List.
Auxiliary Space: O(1)
Deleting nodes at given index in the Circular linked list
Examples:
Input : 99->11->22->33->44->55->66
Index= 4
Output : 99->11->22->33->55->66
Input : 99->11->22->33->44->55->66
Index= 2
Output : 99->11->33->44->55->66
Note: 0-based indexing is considered for the list.

Approach:
- First, find the length of the list. That is, the number of nodes in the list.
- Take two pointers previous and current to traverse the list. Such that previous is one position behind the current node.
- Take a variable count initialized to 0 to keep track of the number of nodes traversed.
- Traverse the list until the given index is reached.
- Once the given index is reached, do previous->next = current->next.
Function to delete a node at given index or location from singly circular linked list:
C++
// Function to delete node at given index
// of Circular Linked List
void DeleteAtPosition(Node* head, int index)
{
// find length of list
int len = Length(head);
int count = 1;
Node* previous = head;
Node* next = head;
// check if list doesn't have any node
// if not then return
if(head == NULL){
cout<<"Delete Last list is empty";
return;
}
// given index is in list or not
if(index >= len || index<0){
cout<<"Index is not Found";
return;
}
// delete first node
if(index == 0){
DeleteFirst(head);
return;
}
// traverse first to last node
while(len > 0){
// if index found delete that node
if(index == count){
previous->next = next->next;
free(next);
return;
}
previous = previous->next;
next = previous->next;
len--;
count++;
}
return;
}
// This code is contributed by Yash Agarwal(yashagarwal2852002)
C
// Function to delete node at given index
// of Circular Linked List
void DeleteAtPosition(struct Node** head, int index)
{
// find length of list
int len = Length(*head);
int count = 1;
struct Node *previous = *head, *next = *head;
// check if list doesn't have any node
// if not then return
if (*head == NULL) {
printf("\nDelete Last List is empty\n");
return;
}
// given index is in list or not
if (index >= len || index < 0) {
printf("\nIndex is not Found\n");
return;
}
// delete first node
if (index == 0) {
DeleteFirst(head);
return;
}
// traverse first to last node
while (len > 0) {
// if index found delete that node
if (index == count) {
previous->next = next->next;
free(next);
return;
}
previous = previous->next;
next = previous->next;
len--;
count++;
}
return;
}
Java
// Java program to delete node at different
// Function delete node at a given position
// of Circular Linked List
static Node DeleteAtPosition( Node head, int index)
{
// Find length of list
int len = Length(head);
int count = 1;
Node previous = head, next = head;
// check list have any node
// if not then return
if (head == null)
{
System.out.printf("\nDelete Last List is empty\n");
return null;
}
// given index is in list or not
if (index >= len || index < 0)
{
System.out.printf("\nIndex is not Found\n");
return null;
}
// delete first node
if (index == 0)
{
head = DeleteFirst(head);
return head;
}
// traverse first to last node
while (len > 0)
{
// if index found delete that node
if (index == count)
{
previous.next = next.next;
return head;
}
previous = previous.next;
next = previous.next;
len--;
count++;
}
return head;
}
// This code is contributed by shivanisinghss2110
Python3
# Function delete node at a given position
# of Circular Linked List
def DeleteAtPosition(head, index):
# Find length of list
len = Length(head)
count = 1
previous = head
next = head
# check list have any node
# if not then return
if (head == None):
print("\nDelete Last List is empty")
return None
# given index is in list or not
if (index >= len or index < 0) :
print("\nIndex is not Found")
return None
# delete first node
if (index == 0) :
head = DeleteFirst(head)
return head
# traverse first to last node
while (len > 0):
# if index found delete that node
if (index == count):
previous.next = next.next
return head
previous = previous.next
next = previous.next
len = len - 1
count = count + 1
return head
# This code is contributed by shivanisinghss2110
C#
// C# program to delete node at given index
// of circular linked list
static void DeleteAtPosition(Node head, int index)
{
// find length of list
int len = Length(head);
int count = 1;
Node previous = head;
Node next = head;
// check if list doesn't have any node
// if noth then return
if (head == null) {
Console.Write("Delete Last list is empty");
return;
}
// given index is in list or not
if (index >= len || index < 0) {
Console.Write("Index is not Found");
return;
}
// delete first node
if (index == 0) {
DeleteFirst(head);
return;
}
// traverse first to last node
while (len > 0) {
// if index found delete that node
if (index == count) {
previous.next = next.next;
return;
}
previous = previous.next;
next = previous.next;
len -= 1;
count++;
}
}
// this code is contributed by Kirti
// Agarwal(kirtiagarwal23121999)
JavaScript
<script>
// Function delete node at a given position
// of Circular Linked List
function DeleteAtPosition(head, index){
// Find length of list
let len = head.length
let count = 1
let previous = head
let next = head
// check list have any node
// if not then return
if (head == null){
document.write("</br>","Delete Last List is empty")
return null
}
// given index is in list or not
if (index >= len || index < 0){
document.write("</br>","Index is not Found")
return null
}
// delete first node
if (index == 0){
head = DeleteFirst(head)
return head
}
// traverse first to last node
while (len > 0){
// if index found delete that node
if (index == count){
previous.next = next.next
return head
}
previous = previous.next
next = previous.next
len = len - 1
count = count + 1
}
return head
}
// This code is contributed by shinjanpatra
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Program implementing all of the above three functions
C++
// C++ program to delete node at different
// positions from a circular linked list
#include <bits/stdc++.h>
using namespace std;
// structure for a node
struct Node {
int data;
struct Node* next;
};
// Function to insert a node at the end of
// a Circular linked list
void Insert(struct Node** head, int data)
{
struct Node* current = *head;
// Create a new node
struct Node* newNode = new Node;
// check node is created or not
if (!newNode) {
printf("\nMemory Error\n");
return;
}
// insert data into newly created node
newNode->data = data;
// check list is empty
// if not have any node then
// make first node it
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
return;
}
// if list have already some node
else {
// move first node to last node
while (current->next != *head) {
current = current->next;
}
// put first or head node address
// in new node link
newNode->next = *head;
// put new node address into last
// node link(next)
current->next = newNode;
}
}
// Function print data of list
void Display(struct Node* head)
{
struct Node* current = head;
// if list is empty, simply show message
if (head == NULL) {
printf("\nDisplay List is empty\n");
return;
}
// traverse first to last node
else {
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
}
}
// Function return number of nodes present in list
int Length(struct Node* head)
{
struct Node* current = head;
int count = 0;
// if list is empty simply return length zero
if (head == NULL) {
return 0;
}
// traverse first to last node
else {
do {
current = current->next;
count++;
} while (current != head);
}
return count;
}
// Function delete First node of Circular Linked List
void DeleteFirst(struct Node** head)
{
struct Node *previous = *head, *next = *head;
// check list have any node
// if not then return
if (*head == NULL) {
printf("\nList is empty\n");
return;
}
// check list have single node
// if yes then delete it and return
if (previous->next == previous) {
*head = NULL;
return;
}
// traverse second to first
while (previous->next != *head) {
previous = previous->next;
next = previous->next;
}
// now previous is last node and
// next is first node of list
// first node(next) link address
// put in last node(previous) link
previous->next = next->next;
// make second node as head node
*head = previous->next;
free(next);
return;
}
// Function to delete last node of
// Circular Linked List
void DeleteLast(struct Node** head)
{
struct Node *current = *head, *temp = *head, *previous;
// check if list doesn't have any node
// if not then return
if (*head == NULL) {
printf("\nList is empty\n");
return;
}
// check if list have single node
// if yes then delete it and return
if (current->next == current) {
*head = NULL;
return;
}
// move first node to last
// previous
while (current->next != *head) {
previous = current;
current = current->next;
}
previous->next = current->next;
*head = previous->next;
free(current);
return;
}
// Function delete node at a given position
// of Circular Linked List
void DeleteAtPosition(struct Node** head, int index)
{
// Find length of list
int len = Length(*head);
int count = 1;
struct Node *previous = *head, *next = *head;
// check list have any node
// if not then return
if (*head == NULL) {
printf("\nDelete Last List is empty\n");
return;
}
// given index is in list or not
if (index >= len || index < 0) {
printf("\nIndex is not Found\n");
return;
}
// delete first node
if (index == 0) {
DeleteFirst(head);
return;
}
// traverse first to last node
while (len > 0) {
// if index found delete that node
if (index == count) {
previous->next = next->next;
free(next);
return;
}
previous = previous->next;
next = previous->next;
len--;
count++;
}
return;
}
// Driver Code
int main()
{
struct Node* head = NULL;
Insert(&head, 99);
Insert(&head, 11);
Insert(&head, 22);
Insert(&head, 33);
Insert(&head, 44);
Insert(&head, 55);
Insert(&head, 66);
// Deleting Node at position
printf("Initial List: ");
Display(head);
printf("\nAfter Deleting node at index 4: ");
DeleteAtPosition(&head, 4);
Display(head);
// Deleting first Node
printf("\n\nInitial List: ");
Display(head);
printf("\nAfter Deleting first node: ");
DeleteFirst(&head);
Display(head);
// Deleting last Node
printf("\n\nInitial List: ");
Display(head);
printf("\nAfter Deleting last node: ");
DeleteLast(&head);
Display(head);
return 0;
}
Java
// Java implementation to delete node at different
// positions from a circular linked list
import java.util.*;
public class GFG {
// structure for a node
static class Node {
int data;
Node next;
};
// Function to insert a node at the end of
// a Circular linked list
static Node Insert(Node head, int data){
Node current = head;
// Create a new node
Node newNode = new Node();
// check node is created or not
if (newNode == null) {
System.out.printf("\nMemory Error\n");
return null;
}
// insert data into newly created node
newNode.data = data;
// check list is empty
// if not have any node then
// make first node it
if (head == null) {
newNode.next = newNode;
head = newNode;
return head;
} else {
// move first node to last node
while (current.next != head) {
current = current.next;
}
// put first or head node address
// in new node link
newNode.next = head;
// put new node address into last
// node link(next)
current.next = newNode;
}
return head;
}
// function to print the data of linked list
static void Display(Node head){
Node current = head;
// if list is empty, simply show message
if (head == null) {
System.out.printf("\nDisplay List is empty\n");
return;
} else {
do {
System.out.printf("%d ", current.data);
current = current.next;
} while (current != head);
}
}
// returns the number of nodes present in the linked list
static int Length(Node head){
Node current = head;
int count = 0;
// if list is empty
// simply return length zero
if (head == null)
return 0;
else {
do {
current = current.next;
count++;
} while (current != head);
}
return count;
}
// function to delete First node of
// circular Linked List
static Node DeleteFirst(Node head){
Node previous = head, next = head;
// check list have any node
// if not then return
if (head == null) {
System.out.printf("\nList is empty\n");
return null;
}
// check list have single node
// if yes then delete it and return
if (previous.next == previous) {
head = null;
return null;
}
// traverse second to first
while (previous.next != head) {
previous = previous.next;
next = previous.next;
}
// now previous is last node and
// next is first node of list
// first node(next) link address
// put in last node(previous) link
previous.next = next.next;
// make second node as head node
head = previous.next;
return head;
}
// Function to delete last node of
// Circular Linked List
static Node DeleteLast(Node head){
Node current = head, temp = head, previous = null;
// check if list doesn't have any node
// if not then return
if (head == null) {
System.out.printf("\nList is empty\n");
return null;
}
// check if list have single node
// if yes then delete it and return
if (current.next == current) {
head = null;
return null;
}
// move first node to last
// previous
while (current.next != head) {
previous = current;
current = current.next;
}
previous.next = current.next;
head = previous.next;
return head;
}
// Function delete node at a given position
// of Circular Linked List
static Node DeleteAtPosition(Node head, int index){
// Find length of list
int len = Length(head);
int count = 1;
Node previous = head, next = head;
// check list have any node
// if not then return
if (head == null) {
System.out.printf(
"\nDelete Last List is empty\n");
return null;
}
// given index is in list or not
if (index >= len || index < 0) {
System.out.printf("\nIndex is not Found\n");
return null;
}
// delete first node
if (index == 0) {
head = DeleteFirst(head);
return head;
}
// traverse first to last node
while (len > 0) {
// if index found delete that node
if (index == count) {
previous.next = next.next;
return head;
}
previous = previous.next;
next = previous.next;
len--;
count++;
}
return head;
}
// Driver Code
public static void main(String args[])
{
Node head = null;
head = Insert(head, 99);
head = Insert(head, 11);
head = Insert(head, 22);
head = Insert(head, 33);
head = Insert(head, 44);
head = Insert(head, 55);
head = Insert(head, 66);
// Deleting Node at position
System.out.printf("Initial List: ");
Display(head);
System.out.printf(
"\nAfter Deleting node at index 4: ");
head = DeleteAtPosition(head, 4);
Display(head);
// Deleting first Node
System.out.printf("\n\nInitial List: ");
Display(head);
System.out.printf("\nAfter Deleting first node: ");
head = DeleteFirst(head);
Display(head);
// Deleting last Node
System.out.printf("\n\nInitial List: ");
Display(head);
System.out.printf("\nAfter Deleting last node: ");
head = DeleteLast(head);
Display(head);
}
}
// THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002)
Python3
# Python3 program to delete node at different
# positions from a circular linked list
# A linked list node
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
self.prev = None
# Function to insert a node at the end of
# a Circular linked list
def Insert(head, data):
current = head
# Create a new node
newNode = Node(0)
# check node is created or not
if (newNode == None):
print("\nMemory Error\n")
return None
# insert data into newly created node
newNode.data = data
# check list is empty
# if not have any node then
# make first node it
if (head == None) :
newNode.next = newNode
head = newNode
return head
# if list have already some node
else:
# move first node to last node
while (current.next != head):
current = current.next
# put first or head node address
# in new node link
newNode.next = head
# put new node address into last
# node link(next)
current.next = newNode
return head
# Function print data of list
def Display(head):
current = head
# if list is empty, simply show message
if (head == None):
print("\nDisplay List is empty\n")
return
# traverse first to last node
else:
while(True):
print( current.data,end=" ")
current = current.next
if (current == head):
break;
# Function return number of nodes present in list
def Length(head):
current = head
count = 0
# if list is empty
# simply return length zero
if (head == None):
return 0
# traverse first to last node
else:
while(True):
current = current.next
count = count + 1
if (current == head):
break;
return count
# Function delete First node of
# Circular Linked List
def DeleteFirst(head):
previous = head
next = head
# check list have any node
# if not then return
if (head == None) :
print("\nList is empty")
return None
# check list have single node
# if yes then delete it and return
if (previous.next == previous) :
head = None
return None
# traverse second to first
while (previous.next != head):
previous = previous.next
next = previous.next
# now previous is last node and
# next is first node of list
# first node(next) link address
# put in last node(previous) link
previous.next = next.next
# make second node as head node
head = previous.next
return head
# Function to delete last node of
# Circular Linked List
def DeleteLast(head):
current = head
temp = head
previous = None
# check if list doesn't have any node
# if not then return
if (head == None):
print("\nList is empty")
return None
# check if list have single node
# if yes then delete it and return
if (current.next == current) :
head = None
return None
# move first node to last
# previous
while (current.next != head):
previous = current
current = current.next
previous.next = current.next
head = previous.next
return head
# Function delete node at a given position
# of Circular Linked List
def DeleteAtPosition(head, index):
# Find length of list
len = Length(head)
count = 1
previous = head
next = head
# check list have any node
# if not then return
if (head == None):
print("\nDelete Last List is empty")
return None
# given index is in list or not
if (index >= len or index < 0) :
print("\nIndex is not Found")
return None
# delete first node
if (index == 0) :
head = DeleteFirst(head)
return head
# traverse first to last node
while (len > 0):
# if index found delete that node
if (index == count):
previous.next = next.next
return head
previous = previous.next
next = previous.next
len = len - 1
count = count + 1
return head
# Driver Code
head = None
head = Insert(head, 99)
head = Insert(head, 11)
head = Insert(head, 22)
head = Insert(head, 33)
head = Insert(head, 44)
head = Insert(head, 55)
head = Insert(head, 66)
# Deleting Node at position
print("Initial List: ")
Display(head)
print("\nAfter Deleting node at index 4: ")
head = DeleteAtPosition(head, 4)
Display(head)
# Deleting first Node
print("\n\nInitial List: ")
Display(head)
print("\nAfter Deleting first node: ")
head = DeleteFirst(head)
Display(head)
# Deleting last Node
print("\n\nInitial List: ")
Display(head)
print("\nAfter Deleting last node: ")
head = DeleteLast(head)
Display(head)
# This code is contributed by Arnab Kundu
C#
// C# program to delete node at different
// positions from a circular linked list
using System;
class GFG
{
// structure for a node
class Node
{
public int data;
public Node next;
};
// Function to insert a node at the end of
// a Circular linked list
static Node Insert(Node head, int data)
{
Node current = head;
// Create a new node
Node newNode = new Node();
// check node is created or not
if (newNode == null)
{
Console.Write("\nMemory Error\n");
return null;
}
// insert data into newly created node
newNode.data = data;
// check list is empty
// if not have any node then
// make first node it
if (head == null)
{
newNode.next = newNode;
head = newNode;
return head;
}
// if list have already some node
else
{
// move first node to last node
while (current.next != head)
{
current = current.next;
}
// put first or head node address
// in new node link
newNode.next = head;
// put new node address into last
// node link(next)
current.next = newNode;
}
return head;
}
// Function print data of list
static void Display( Node head)
{
Node current = head;
// if list is empty, simply show message
if (head == null)
{
Console.Write("\nDisplay List is empty\n");
return;
}
// traverse first to last node
else
{
do
{
Console.Write("{0} ", current.data);
current = current.next;
} while (current != head);
}
}
// Function return number of nodes present in list
static int Length(Node head)
{
Node current = head;
int count = 0;
// if list is empty
// simply return length zero
if (head == null)
{
return 0;
}
// traverse first to last node
else
{
do
{
current = current.next;
count++;
} while (current != head);
}
return count;
}
// Function delete First node of
// Circular Linked List
static Node DeleteFirst(Node head)
{
Node previous = head, next = head;
// check list have any node
// if not then return
if (head == null)
{
Console.Write("\nList is empty\n");
return null;
}
// check list have single node
// if yes then delete it and return
if (previous.next == previous)
{
head = null;
return null;
}
// traverse second to first
while (previous.next != head)
{
previous = previous.next;
next = previous.next;
}
// now previous is last node and
// next is first node of list
// first node(next) link address
// put in last node(previous) link
previous.next = next.next;
// make second node as head node
head = previous.next;
return head;
}
// Function to delete last node of
// Circular Linked List
static Node DeleteLast(Node head)
{
Node current = head, temp = head, previous = null;
// check if list doesn't have any node
// if not then return
if (head == null)
{
Console.Write("\nList is empty\n");
return null;
}
// check if list have single node
// if yes then delete it and return
if (current.next == current)
{
head = null;
return null;
}
// move first node to last
// previous
while (current.next != head)
{
previous = current;
current = current.next;
}
previous.next = current.next;
head = previous.next;
return head;
}
// Function delete node at a given position
// of Circular Linked List
static Node DeleteAtPosition( Node head, int index)
{
// Find length of list
int len = Length(head);
int count = 1;
Node previous = head, next = head;
// check list have any node
// if not then return
if (head == null)
{
Console.Write("\nDelete Last List is empty\n");
return null;
}
// given index is in list or not
if (index >= len || index < 0)
{
Console.Write("\nIndex is not Found\n");
return null;
}
// delete first node
if (index == 0)
{
head = DeleteFirst(head);
return head;
}
// traverse first to last node
while (len > 0)
{
// if index found delete that node
if (index == count)
{
previous.next = next.next;
return head;
}
previous = previous.next;
next = previous.next;
len--;
count++;
}
return head;
}
// Driver Code
public static void Main(String []args)
{
Node head = null;
head = Insert(head, 99);
head = Insert(head, 11);
head = Insert(head, 22);
head = Insert(head, 33);
head = Insert(head, 44);
head = Insert(head, 55);
head = Insert(head, 66);
// Deleting Node at position
Console.Write("Initial List: ");
Display(head);
Console.Write("\nAfter Deleting node at index 4: ");
head = DeleteAtPosition(head, 4);
Display(head);
// Deleting first Node
Console.Write("\n\nInitial List: ");
Display(head);
Console.Write("\nAfter Deleting first node: ");
head = DeleteFirst(head);
Display(head);
// Deleting last Node
Console.Write("\n\nInitial List: ");
Display(head);
Console.Write("\nAfter Deleting last node: ");
head = DeleteLast(head);
Display(head);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript program to delete node at different
// positions from a circular linked list
// A linked list node
class Node{
constructor(new_data){
this.data = new_data
this.next = null
this.prev = null
}
}
// Function to insert a node at the end of
// a Circular linked list
function Insert(head, data){
let current = head
// Create a new node
let newNode = new Node(0)
// check node is created or not
if (newNode == null){
document.write("</br>","Memory Error","</br>")
return null
}
// insert data into newly created node
newNode.data = data
// check list is empty
// if not have any node then
// make first node it
if (head == null){
newNode.next = newNode
head = newNode
return head
}
// if list have already some node
else{
// move first node to last node
while (current.next != head)
current = current.next
// put first or head node address
// in new node link
newNode.next = head
// put new node address into last
// node link(next)
current.next = newNode
}
return head
}
// Function document.write data of list
function Display(head){
let current = head
// if list is empty, simply show message
if (head == null){
document.write("</br>","Display List is empty","</br>")
return
}
// traverse first to last node
else{
while(true){
document.write( current.data," ")
current = current.next
if (current == head)
break;
}
}
}
// Function return number of nodes present in list
function Length(head){
let current = head
let count = 0
// if list is empty
// simply return length zero
if (head == null)
return 0
// traverse first to last node
else{
while(true){
current = current.next
count = count + 1
if (current == head)
break;
}
}
return count
}
// Function delete First node of
// Circular Linked List
function DeleteFirst(head){
let previous = head
let next = head
// check list have any node
// if not then return
if (head == null){
document.write("</br>","List is empty")
return null
}
// check list have single node
// if yes then delete it and return
if (previous.next == previous){
head = null
return null
}
// traverse second to first
while (previous.next != head){
previous = previous.next
next = previous.next
}
// now previous is last node and
// next is first node of list
// first node(next) link address
// put in last node(previous) link
previous.next = next.next
// make second node as head node
head = previous.next
return head
}
// Function to delete last node of
// Circular Linked List
function DeleteLast(head){
let current = head
let temp = head
let previous = null
// check if list doesn't have any node
// if not then return
if (head == null){
document.write("</br>","List is empty")
return null
}
// check if list have single node
// if yes then delete it and return
if (current.next == current){
head = null
return null
}
// move first node to last
// previous
while (current.next != head){
previous = current
current = current.next
}
previous.next = current.next
head = previous.next
return head
}
// Function delete node at a given position
// of Circular Linked List
function DeleteAtPosition(head, index){
// Find length of list
len = Length(head)
count = 1
previous = head
next = head
// check list have any node
// if not then return
if (head == null){
document.write("</br>","Delete Last List is empty")
return null
}
// given index is in list or not
if (index >= len || index < 0) {
document.write("</br>","Index is not Found")
return null
}
// delete first node
if (index == 0){
head = DeleteFirst(head)
return head
}
// traverse first to last node
while (len > 0){
// if index found delete that node
if (index == count){
previous.next = next.next
return head
}
previous = previous.next
next = previous.next
len = len - 1
count = count + 1
}
return head
}
// Driver Code
let head = null
head = Insert(head, 99)
head = Insert(head, 11)
head = Insert(head, 22)
head = Insert(head, 33)
head = Insert(head, 44)
head = Insert(head, 55)
head = Insert(head, 66)
// Deleting Node at position
document.write("Initial List: ")
Display(head)
document.write("</br>","After Deleting node at index 4: ")
head = DeleteAtPosition(head, 4)
Display(head)
// Deleting first Node
document.write("</br>","</br>","Initial List: ")
Display(head)
document.write("</br>","After Deleting first node: ")
head = DeleteFirst(head)
Display(head)
// Deleting last Node
document.write("</br>","</br>","Initial List: ")
Display(head)
document.write("</br>","After Deleting last node: ")
head = DeleteLast(head)
Display(head)
// This code is contributed by shinjanpatra
</script>
OutputInitial List: 99 11 22 33 44 55 66
After Deleting node at index 4: 99 11 22 33 55 66
Initial List: 99 11 22 33 55 66
After Deleting first node: 11 22 33 55 66
Initial List: 11 22 33 55 66
After Deleting last node: 11 22 33 55
Time Complexity: O(N) where N is the number of nodes in given linked list.
Auxiliary Space: O(1)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem