The Wayback Machine - https://web.archive.org/web/20240927160233/https://www.geeksforgeeks.org/deletion-circular-linked-list/
Open In App

Deletion from a Circular Linked List

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will learn how to delete a node from a circular linked list. In a circular linked list, the last node connects back to the first node, creating a loop.

There are three main ways to delete a node from circular linked list:

  • Deletion at the beginning
  • Deletion at specific position
  • Deletion at the end

Now, let’s look at the methods and steps for these three deletion operations.

Deletion from a Circular Linked List:

Deletion involves removing a node from the linked list. The main difference is that we need to ensure the list remains circular after the deletion. We can delete a node in a circular linked list in three ways:

1. Deletion from the beginning of the circular linked list

To delete the first node of a circular linked list, we first check if the list is empty. If it is then we print a message and return NULL. If the list contains only one node (the head is the same as the last) then we delete that node and set the last pointer to NULL. If there are multiple nodes then we update the last->next pointer to skip the head node and effectively removing it from the list. We then delete the head node to free the allocated memory. Finally, we return the updated last pointer, which still points to the last node in the list.

Deletion-from-the-beginning-of-circular-linked-list

Delete the first node in circular linked list

Step-by-step approach:

  • Check if List is Empty:
    • If last is nullptr, print “List is empty” and return nullptr.
  • Get Head Node:
    • Set head to last->next.
  • Check for Single Node:
    • If head equals last, delete head and set last to nullptr.
  • Handle Multiple Nodes:
    • Update last->next to point to head->next.
    • Delete head.
  • Return Updated last

Below is the implementation of the above approach:

C++
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
    Node(int value) {
        data = value;
        next = nullptr;
    }
};

// Function to delete the first node of the circular linked list
Node* deleteFirstNode(Node* last) {
    if (last == nullptr) {
        // If the list is empty
        cout << "List is empty" << endl;
        return nullptr;
    }

    Node* head = last->next;

    if (head == last) {
        // If there is only one node in the list
        delete head;
        last = nullptr;
    } else {
        // More than one node in the list
        last->next = head->next;
        delete head;
    }

    return last;
}

void printList(Node* last) {
    if(last == NULL) return ;
 
    Node *head = last->next;
    while (true){
        cout << head->data << " ";
        head = head->next;
        if (head == last->next) break;
    }
    cout << endl;
}

int main() {
    // Create circular linked list: 2, 3, 4
    Node* first = new Node(2);
    first->next = new Node(3);
    first->next->next = new Node(4);

    Node* last = first->next->next;
    last->next = first;

    cout << "Original list: ";
    printList(last);

    // Delete the first node
    last = deleteFirstNode(last);

    cout << "List after deleting first node: ";
    printList(last);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* deleteFirstNode(struct Node* last) {
    if (last == NULL) {
        // If the list is empty
        printf("List is empty\n");
        return NULL;
    }

    struct Node* head = last->next;

    if (head == last) {
        // If there is only one node in the list
        free(head);
        last = NULL;
    } else {
        // More than one node in the list
        last->next = head->next;
        free(head);
    }

    return last;
}

void printList(struct Node* last) {
    if (last == NULL) return;

    struct Node* head = last->next;
    while (1) {
        printf("%d ", head->data);
        head = head->next;
        if (head == last->next) break;
    }
    printf("\n");
}

struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

int main() {
    struct Node* first = createNode(2);
    first->next = createNode(3);
    first->next->next = createNode(4);

    struct Node* last = first->next->next;
    last->next = first;

    printf("Original list: ");
    printList(last);

    last = deleteFirstNode(last);

    printf("List after deleting first node: ");
    printList(last);

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int value) {
        data = value;
        next = null;
    }
}

public class GFG {
    public static Node deleteFirstNode(Node last) {
        if (last == null) {
            // If the list is empty
            System.out.println("List is empty");
            return null;
        }

        Node head = last.next;

        if (head == last) {
            // If there is only one node in the list
            last = null;
        } else {
            // More than one node in the list
            last.next = head.next;
        }

        return last;
    }

    public static void printList(Node last) {
        if (last == null) return;

        Node head = last.next;
        while (true) {
            System.out.print(head.data + " ");
            head = head.next;
            if (head == last.next) break;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        // Create circular linked list: 2, 3, 4
        Node first = new Node(2);
        first.next = new Node(3);
        first.next.next = new Node(4);

        Node last = first.next.next;
        last.next = first;

        System.out.print("Original list: ");
        printList(last);

        // Delete the first node
        last = deleteFirstNode(last);

        System.out.print("List after deleting first node: ");
        printList(last);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def deleteFirstNode(last):
    if last is None:
        # If the list is empty
        print("List is empty")
        return None

    head = last.next

    if head == last:
        # If there is only one node in the list
        last = None
    else:
        # More than one node in the list
        last.next = head.next

    return last

def print_list(last):
    if last is None:
        return

    head = last.next
    while True:
        print(head.data, end=" ")
        head = head.next
        if head == last.next:
            break
    print()

# Create circular linked list: 2, 3, 4
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)

last = first.next.next
last.next = first

print("Original list: ", end="")
print_list(last)

# Delete the first node
last = deleteFirstNode(last)

print("List after deleting first node: ", end="")
print_list(last)
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function deleteFirstNode(last) {
    if (last === null) {
        // If the list is empty
        console.log("List is empty");
        return null;
    }

    let head = last.next;

    if (head === last) {
        // If there is only one node in the list
        last = null;
    } else {
        // More than one node in the list
        last.next = head.next;
    }

    return last;
}

function printList(last) {
    if (last === null) return;

    let head = last.next;
    while (true) {
        console.log(head.data + " ");
        head = head.next;
        if (head === last.next) break;
    }
    console.log();
}

// Create circular linked list: 2, 3, 4
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);

let last = first.next.next;
last.next = first;

console.log("Original list: ");
printList(last);

// Delete the first node
last = deleteFirstNode(last);

console.log("List after deleting first node: ");
printList(last);

Output
Original list: 2 3 4 
List after deleting first node: 3 4 

Time Complexity: O(1)
Auxiliary Space: O(1)

2. Deletion at specific position in circular linked list

To delete a specific node from a circular linked list, we first check if the list is empty. If it is then we print a message and return nullptr. If the list contains only one node and it matches the key then we delete that node and set last to nullptr. If the node to be deleted is the first node then we update the next pointer of the last node to skip the head node and delete the head. For other nodes, we traverse the list using two pointers: curr (to find the node) and prev (to keep track of the previous node). If we find the node with the matching key then we update the next pointer of prev to skip the curr node and delete it. If the node is found and it is the last node, we update the last pointer accordingly. If the node is not found then do nothing and tail or last as it is. Finally, we return the updated last pointer.

Delete-a-specific-node-in-circular-linked-list

Delete a specific node in circular linked list

Step-by-step approach:

  • Check if List is Empty (last is nullptr) then print “List is empty, nothing to delete” and return nullptr.
  • Set curr to last->next (head) and prev to last.
  • Check for single node, if node’s data matches key, delete it and set last to nullptr.
  • Check for first node to delete, if head node’s data matches key, update last->next and delete the head.
  • Loop through the list to find the node with the specified key.
    • If node found then update prev->next to skip the deleted node. If the deleted node is last, update last to prev.
    • Otherwise, print “Node with data [key] not found.”
  • Return the modified last.

Below is the implementation of the above approach:

C++
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
    Node(int value) {
        data = value;
        next = nullptr;
    }
};

// Function to delete a specific node in the circular linked list
Node* deleteSpecificNode(Node* last, int key) {
    if (last == nullptr) {
        // If the list is empty
        cout << "List is empty, nothing to delete." << endl;
        return nullptr;
    }

    Node* curr = last->next;
    Node* prev = last;

    // If the node to be deleted is the only node in the list
    if (curr == last && curr->data == key) {
        delete curr;
        last = nullptr;
        return last;
    }

    // If the node to be deleted is the first node
    if (curr->data == key) {
        last->next = curr->next;
        delete curr;
        return last;
    }

    // Traverse the list to find the node to be deleted
    while (curr != last && curr->data != key) {
        prev = curr;
        curr = curr->next;
    }

    // If the node to be deleted is found
    if (curr->data == key) {
        prev->next = curr->next;
        if (curr == last) {
            last = prev;
        }
        delete curr;
    } else {
        // If the node to be deleted is not found
        cout << "Node with data " << key
          << " not found." << endl;
    }

    return last;
}

// Function to print the circular linked list
void printList(Node* last) {
     if (last == NULL){
        cout << "List is Empty";
        return;
    }

    Node *head = last->next;
    while (true){
        cout << head->data << " ";
        head = head->next;
        if (head == last->next) break;
    }
    cout << endl;
}

int main() {
    // Create circular linked list: 2, 3, 4
    Node* first = new Node(2);
    first->next = new Node(3);
    first->next->next = new Node(4);

    Node* last = first->next->next;
    last->next = first;

    cout << "Original list: ";
    printList(last);

    // Delete a specific node
    int key = 3;
    last = deleteSpecificNode(last, key);

    cout << "List after deleting node " << key << ": ";
    printList(last);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in the circular linked list
struct Node {
    int data;
    struct Node* next;
};

// Function to delete a specific node in the circular linked list
struct Node* deleteSpecificNode(struct Node* last, int key) {
    if (last == NULL) {
        // If the list is empty
        printf("List is empty, nothing to delete.\n");
        return NULL;
    }

    struct Node* curr = last->next;
    struct Node* prev = last;

    // If the node to be deleted is the only node in the list
    if (curr == last && curr->data == key) {
        free(curr);
        last = NULL;
        return last;
    }

    // If the node to be deleted is the first node
    if (curr->data == key) {
        last->next = curr->next;
        free(curr);
        return last;
    }

    // Traverse the list to find the node to be deleted
    while (curr != last && curr->data != key) {
        prev = curr;
        curr = curr->next;
    }

    // If the node to be deleted is found
    if (curr->data == key) {
        prev->next = curr->next;
        if (curr == last) {
            last = prev;
        }
        free(curr);
    } else {
        // If the node to be deleted is not found
        printf("Node with data %d not found.\n", key);
    }

    return last;
}

void printList(struct Node* last) {
    if (last == NULL) {
        printf("List is Empty");
        return;
    }

    struct Node* head = last->next;
    while (1) {
        printf("%d ", head->data);
        head = head->next;
        if (head == last->next) break;
    }
    printf("\n");
}

struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

int main() {
    // Create circular linked list: 2, 3, 4
    struct Node* first = createNode(2);
    first->next = createNode(3);
    first->next->next = createNode(4);

    struct Node* last = first->next->next;
    last->next = first;

    printf("Original list: ");
    printList(last);

    // Delete a specific node
    int key = 3;
    last = deleteSpecificNode(last, key);

    printf("List after deleting node %d: ", key);
    printList(last);

    return 0;
}
Java
class Node {
    int data;
    Node next;
    Node(int value){
        data = value;
        next = null;
    }
}

public class GFG {
    public static Node deleteSpecificNode(Node last,
                                          int key){
        if (last == null) {
            // If the list is empty
            System.out.println(
                "List is empty, nothing to delete.");
            return null;
        }
        Node curr = last.next;
        Node prev = last;

        // If the node to be deleted is the only node in the
        // list
        if (curr == last && curr.data == key) {
            last = null;
            return last;
        }

        // If the node to be deleted is the first node
        if (curr.data == key) {
            last.next = curr.next;
            return last;
        }

        // Traverse the list to find the node to be deleted
        while (curr != last && curr.data != key) {
            prev = curr;
            curr = curr.next;
        }

        // If the node to be deleted is found
        if (curr.data == key) {
            prev.next = curr.next;
            if (curr == last) {
                last = prev;
            }
        }
        else {
            // If the node to be deleted is not found
            System.out.println("Node with data " + key
                               + " not found.");
        }
        return last;
    }

    public static void printList(Node last){
        if (last == null) {
            System.out.println("List is Empty");
            return;
        }
      
        Node head = last.next;
        while (true) {
            System.out.print(head.data + " ");
            head = head.next;
            if (head == last.next)
                break;
        }
        System.out.println();
    }

    public static void main(String[] args){
        // Create circular linked list: 2, 3, 4
        Node first = new Node(2);
        first.next = new Node(3);
        first.next.next = new Node(4);

        Node last = first.next.next;
        last.next = first;

        System.out.print("Original list: ");
        printList(last);

        // Delete a specific node
        int key = 3;
        last = deleteSpecificNode(last, key);

        System.out.print("List after deleting node " + key
                         + ": ");
        printList(last);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def deleteSpecificNode(last, key):
    if last is None:
        # If the list is empty
        print("List is empty, nothing to delete.")
        return None

    curr = last.next
    prev = last

    # If the node to be deleted is the only node in the list
    if curr == last and curr.data == key:
        last = None
        return last

    # If the node to be deleted is the first node
    if curr.data == key:
        last.next = curr.next
        return last

    # Traverse the list to find the node to be deleted
    while curr != last and curr.data != key:
        prev = curr
        curr = curr.next

    # If the node to be deleted is found
    if curr.data == key:
        prev.next = curr.next
        if curr == last:
            last = prev
    else:
        # If the node to be deleted is not found
        print(f"Node with data {key} not found.")

    return last

def printList(last):
    if last is None:
        print("List is Empty")
        return

    head = last.next
    while True:
        print(head.data, end=" ")
        head = head.next
        if head == last.next:
            break
    print()

# Create circular linked list: 2, 3, 4
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)

last = first.next.next
last.next = first

print("Original list: ", end="")
printList(last)

# Delete a specific node
key = 3
last = deleteSpecificNode(last, key)

print(f"List after deleting node {key}: ", end="")
printList(last)
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function deleteSpecificNode(last, key) {
    if (last === null) {
        // If the list is empty
        console.log("List is empty, nothing to delete.");
        return null;
    }

    let curr = last.next;
    let prev = last;

    // If the node to be deleted is the only node in the list
    if (curr === last && curr.data === key) {
        last = null;
        return last;
    }

    // If the node to be deleted is the first node
    if (curr.data === key) {
        last.next = curr.next;
        return last;
    }

    // Traverse the list to find the node to be deleted
    while (curr !== last && curr.data !== key) {
        prev = curr;
        curr = curr.next;
    }

    // If the node to be deleted is found
    if (curr.data === key) {
        prev.next = curr.next;
        if (curr === last) {
            last = prev;
        }
    } else {
        // If the node to be deleted is not found
        console.log("Node with data " + key + " not found.");
    }

    return last;
}

function printList(last) {
    if (last === null) {
        console.log("List is Empty");
        return;
    }

    let head = last.next;
    while (true) {
        console.log(head.data + " ");
        head = head.next;
        if (head === last.next) break;
    }
    console.log();
}

// Create circular linked list: 2, 3, 4
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);

let last = first.next.next;
last.next = first;

console.log("Original list: ");
printList(last);

// Delete a specific node
let key = 3;
last = deleteSpecificNode(last, key);

console.log("List after deleting node " + key + ": ");
printList(last);

Output
Original list: 2 3 4 
List after deleting node 3: 2 4 

Time Complexity: O(n), due to traversing the list to find the specific position
Auxiliary Space: O(1)

3. Deletion at the end of Circular linked list

To delete the last node in a circular linked list, we first check if the list is empty. If it is, we print a message and return nullptr. If the list contains only one node (where the head is the same as the last), we delete that node and set last to nullptr. For lists with multiple nodes, we need to traverse the list to find the second last node. We do this by starting from the head and moving through the list until we reach the node whose next pointer points to last. Once we find the second last node then we update its next pointer to point back to the head, this effectively removing the last node from the list. We then delete the last node to free up memory and return the updated last pointer, which now points to the last node.

Deletion-at-the-end-of-circular-linked-list

Deletion at the end of Circular linked list

Step-by-step approach:

  • Check if List is Empty (last is nullptr) then print “List is empty, nothing to delete” and return nullptr.
  • Get head node by Setting head to last->next.
  • Check for single node, if head equals last, delete last and set last to nullptr.
  • Find second last node by traversing the list until curr->next equals last.
  • Update Pointer by setting curr->next to point to head.
  • Delete last and update last to curr.
  • Return the updated last.

Below is the implementation of the above approach:

C++
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
    Node(int value) {
        data = value;
        next = nullptr;
    }
};

// Function to delete the last node in the circular linked list
Node* deleteLastNode(Node* last) {
    if (last == nullptr) {
        // If the list is empty
        cout << "List is empty, nothing to delete." << endl;
        return nullptr;
    }
    Node* head = last->next;

    // If there is only one node in the list
    if (head == last) {
        delete last;
        last = nullptr;
        return last;
    }
    // Traverse the list to find the second last node
    Node* curr = head;
    while (curr->next != last) {
        curr = curr->next;
    }
    // Update the second last node's next pointer
    // to point to head
    curr->next = head;
    delete last;
    last = curr;

    return last;
}

void printList(Node* last) {
   if(last == NULL) return;

    Node *head = last->next;
    while (true){
        cout << head->data << " ";
        head = head->next;
        if (head == last->next) break;
    }
    cout << endl;
}

int main() {
    // Create circular linked list: 2, 3, 4
    Node* first = new Node(2);
    first->next = new Node(3);
    first->next->next = new Node(4);

    Node* last = first->next->next;
    last->next = first;

    cout << "Original list: ";
    printList(last);

    // Delete the last node
    last = deleteLastNode(last);

    cout << "List after deleting last node: ";
    printList(last);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in the circular linked list
struct Node {
    int data;
    struct Node* next;
};

// Function to delete the last node in the circular linked list
struct Node* deleteLastNode(struct Node* last) {
    if (last == NULL) {
        // If the list is empty
        printf("List is empty, nothing to delete.\n");
        return NULL;
    }
    struct Node* head = last->next;

    // If there is only one node in the list
    if (head == last) {
        free(last);
        last = NULL;
        return last;
    }
    // Traverse the list to find the second last node
    struct Node* curr = head;
    while (curr->next != last) {
        curr = curr->next;
    }
    // Update the second last node's next pointer to point to head
    curr->next = head;
    free(last);
    last = curr;

    return last;
}

void printList(struct Node* last) {
    if (last == NULL) return;

    struct Node* head = last->next;
    while (1) {
        printf("%d ", head->data);
        head = head->next;
        if (head == last->next) break;
    }
    printf("\n");
}

struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

int main() {
    // Create circular linked list: 2, 3, 4
    struct Node* first = createNode(2);
    first->next = createNode(3);
    first->next->next = createNode(4);

    struct Node* last = first->next->next;
    last->next = first;

    printf("Original list: ");
    printList(last);

    // Delete the last node
    last = deleteLastNode(last);

    printf("List after deleting last node: ");
    printList(last);

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int value){
        data = value;
        next = null;
    }
}

public class GFG {
    public static Node deleteLastNode(Node last){
        if (last == null) {
            // If the list is empty
            System.out.println(
                "List is empty, nothing to delete.");
            return null;
        }
        Node head = last.next;

        // If there is only one node in the list
        if (head == last) {
            last = null;
            return last;
        }
        // Traverse the list to find the second last node
        Node curr = head;
        while (curr.next != last) {
            curr = curr.next;
        }
        // Update the second last node's next pointer to
        // point to head
        curr.next = head;
        last = curr;

        return last;
    }

    public static void printList(Node last){
        if (last == null)
            return;

        Node head = last.next;
        while (true) {
            System.out.print(head.data + " ");
            head = head.next;
            if (head == last.next)
                break;
        }
        System.out.println();
    }

    public static void main(String[] args){
        // Create circular linked list: 2, 3, 4
        Node first = new Node(2);
        first.next = new Node(3);
        first.next.next = new Node(4);

        Node last = first.next.next;
        last.next = first;

        System.out.print("Original list: ");
        printList(last);

        // Delete the last node
        last = deleteLastNode(last);

        System.out.print("List after deleting last node: ");
        printList(last);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def deleteLastNode(last):
    if last is None:
        # If the list is empty
        print("List is empty, nothing to delete.")
        return None

    head = last.next

    # If there is only one node in the list
    if head == last:
        last = None
        return last

    # Traverse the list to find the second last node
    curr = head
    while curr.next != last:
        curr = curr.next

    # Update the second last node's next pointer to point to head
    curr.next = head
    last = curr

    return last

def printList(last):
    if last is None:
        return

    head = last.next
    while True:
        print(head.data, end=" ")
        head = head.next
        if head == last.next:
            break
    print()

# Create circular linked list: 2, 3, 4
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)

last = first.next.next
last.next = first

print("Original list: ", end="")
printList(last)

# Delete the last node
last = deleteLastNode(last)

print("List after deleting last node: ", end="")
printList(last)
C#
using System;

public class Node {
    public int data;
    public Node next;

    public Node(int value)
    {
        data = value;
        next = null;
    }
}

public class GFG {
    // Function to delete the last node in the circular
    // linked list
    public static Node deleteLastNode(Node last)
    {
        if (last == null) {
            // If the list is empty
            Console.WriteLine(
                "List is empty, nothing to delete.");
            return null;
        }
        Node head = last.next;

        // If there is only one node in the list
        if (head == last) {
            last = null;
            return last;
        }
        // Traverse the list to find the second last node
        Node curr = head;
        while (curr.next != last) {
            curr = curr.next;
        }
        // Update the second last node's next pointer
        // to point to head
        curr.next = head;
        last = curr;

        return last;
    }

    // Function to print the circular linked list
    public static void printList(Node last)
    {
        if (last == null) {
            Console.WriteLine("List is Empty");
            return;
        }

        Node head = last.next;
        while (true) {
            Console.Write(head.data + " ");
            head = head.next;
            if (head == last.next)
                break;
        }
        Console.WriteLine();
    }

    public static void Main(string[] args)
    {
        // Create circular linked list: 2, 3, 4
        Node first = new Node(2);
        first.next = new Node(3);
        first.next.next = new Node(4);

        Node last = first.next.next;
        last.next = first;

        Console.Write("Original list: ");
        printList(last);

        // Delete the last node
        last = deleteLastNode(last);

        Console.Write("List after deleting last node: ");
        printList(last);
    }
}
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function deleteLastNode(last) {
    if (last === null) {
        // If the list is empty
        console.log("List is empty, nothing to delete.");
        return null;
    }
    let head = last.next;

    // If there is only one node in the list
    if (head === last) {
        last = null;
        return last;
    }
    // Traverse the list to find the second last node
    let curr = head;
    while (curr.next !== last) {
        curr = curr.next;
    }
    // Update the second last node's next pointer to point to head
    curr.next = head;
    last = curr;

    return last;
}

function printList(last) {
    if (last === null) return;

    let head = last.next;
    while (true) {
        process.stdout.write(head.data + " ");
        head = head.next;
        if (head === last.next) break;
    }
    console.log();
}

// Create circular linked list: 2, 3, 4
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);

let last = first.next.next;
last.next = first;

console.log("Original list: ");
printList(last);

// Delete the last node
last = deleteLastNode(last);

console.log("List after deleting last node: ");
printList(last);

Output
Original list: 2 3 4 
List after deleting last node: 2 3 

Time Complexity: O(1)
Auxiliary Space: O(1)



Similar Reads

Deletion at different positions in a Circular Linked List
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 [Tex]i [/Tex].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-
15+ min read
Deletion in Doubly Circular Linked List
We have discussed the doubly circular linked list introduction and its insertion.Let us formulate the problem statement to understand the deletion process. Given a ‘key’, delete the first occurrence of this key in the circular doubly linked list. Algorithm: Case 1: Empty List(start = NULL) If the list is empty, simply return it. Case 2: The List in
15+ min read
Circular Linked List Implementation of Circular Queue
Prerequisite – Circular Singly Linked List We have discussed basics and how to implement circular queue using array in set 1. Circular Queue | Set 1 (Introduction and Array Implementation) In this post another method of circular queue implementation is discussed, using Circular Singly Linked List. Operations on Circular Queue: Front:Get the front i
9 min read
Check if a linked list is Circular Linked List
Given the head of a singly linked list, the task is to find if given linked list is circular or not. A linked list is called circular if its last node points back to its first node. Note: The linked list does not contain any internal loops. Example: Input: LinkedList: 2->4->6->7->5 Output: trueExplanation: As shown in figure the first a
12 min read
Convert singly linked list into circular linked list
Given a singly linked list, the task is to convert it into a circular linked list. Examples: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: Explanation: Singly linked list is converted to circular by pointing last node to head. Input: head: 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> NULLOutput: Explanation: Singly l
11 min read
Deletion at end (Removal of last node) in a Linked List
Given a linked list, the task is to delete the last node of the given linked list. Examples:   Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULLOutput: 1 -> 2 -> 3 -> 4 -> NULL Explanation: The last node of the linked list is 5, so 5 is deleted. Input: 3 -> 12 -> 15-> NULLOutput: 3 -> 12 -> NULL Explanation: The last no
8 min read
Deletion at beginning (Removal of first node) in a Doubly Linked List
Given a doubly linked list, the task is to delete the node from the beginning of the linked list. Examples: Input : 1 <-> 2 <-> 3 -> NULLOutput : 2 <-> 3 <-> NULL Input : 2 <-> 4 <-> 6 <-> 8 <-> 33 <-> 67 <-> NULLOutput : 4 <-> 6 <-> 8 <-> 33 <-> 67 <-> NUL
7 min read
Deletion at end (Removal of last node) in a Doubly Linked List
Given a doubly linked list, the task is to delete the last node of the given linked list. Examples: Input: 1 <-> 2 <-> 3 <-> NULLOutput: 1 <-> 2 <-> NULLExplanation: The last node of the linked list is 3, so 3 is deleted. Input: 15 -> NULLOutput: NULLExplanation: The last node of the linked list is 15, so 15 is dele
7 min read
Deletion at beginning (Removal of first node) in a Linked List
Given a linked list, The task is to remove the first node from the given linked list. Examples: Input : head : 3 -> 12 -> 15 -> 18 -> NULLOutput : 12 -> 15 -> 18 -> NULL Input : head : 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULLOutput : 4 -> 6 -> 8 -> 33 -> 67 -> NULL By Shifting head node to next node
6 min read
Deletion in a Doubly Linked List
Deleting a node in a doubly linked list is very similar to deleting a node in a singly linked list. However, there is a little extra work required to maintain the links of both the previous and next nodes. In this article, we will learn about different ways to delete a node in a doubly linked list. Example: Input: DLL = 1 <-> 2 <->3 , N
15+ min read
Deletion in Linked List
Deleting a node in a Linked List is an important operation and can be done in three main ways: removing the first node, removing a node in the middle, or removing the last node. In this article, we will explore deletion operation on Linked List for all the above scenarios. Types of Deletion in Linked ListTable of Content 1. Deletion at the Beginnin
15+ min read
Insertion in an empty List in the circular linked list
A circular linked list is a type of data structure where each node points to the next one, and the last node connects back to the first, forming a circle. This setup allows you to loop through the list without stopping. Knowing how to insert a node into an empty circular linked list is important in creating circular linked list. Insertion in an emp
5 min read
What is Circular Queue | Circular Queue meaning
A circular queue is an extended version of regular queue in which the last element of the queue is connected to the first element of the queue forming a cycle. Properties of Circular Queue: Along with the properties of a regular queue the circular queue has som other unique properties as mentioned below: Front and rear pointers: Two pointers, one a
4 min read
Circular Linked List meaning in DSA
A circular linked list is a special type of linked list in which the last node is connected to the first node, creating a continuous loop. In a circular linked list, each node has a reference to the next node in the sequence, just like in a regular linked list, but the last node's reference points back to the first node. Characteristics of Circular
3 min read
Applications, Advantages and Disadvantages of Circular Doubly Linked List
The circular doubly linked list is a combination of the doubly linked list and the circular linked list. It means that this linked list is bidirectional and contains two pointers and the last pointer points to the first pointer. Applications of Circular Doubly Linked List: Music and video playlists: Circular doubly linked lists are commonly used to
4 min read
Convert an Array to a Circular Doubly Linked List
Prerequisite: Doubly Linked list, Circular Linked List, Circular Doubly Linked ListGiven an array of N elements. The task is to write a program to convert the array into a circular doubly linked list. The idea is to start traversing the array and for every array element create a new list node and assign the prev and next pointers of this node accor
8 min read
Reverse a doubly circular linked list
The problem is to reverse the given doubly circular linked list. Examples: Input: Output: Algorithm: insertEnd(head, new_node) Declare last if head == NULL then new_node->next = new_node->prev = new_node head = new_node return last = head->prev new_node->next = head head->prev = new_node new_node->prev = last last->next = new_n
11 min read
Search an Element in Doubly Circular Linked List
Pre-requisite: Convert an Array to a Circular Doubly Linked List, Doubly Circular Linked ListGiven a doubly circular linked list. The task is to find the position of an element in the list. Image Representation: Algorithm: Declare a temp pointer, and initialize it to the head of the list.Iterate the loop until temp reaches the start address (last n
13 min read
Delete every Kth node from circular linked list
Delete every kth node from a circular linked list until only one node is left. Also, print the intermediate lists. Examples: Input : n=4, k=2, list = 1->2->3->4 Output : 1->2->3->4->1 1->2->4->1 2->4->2 2->2 Input : n=9, k=4, list = 1->2->3->4->5->6->7->8->9 Output : 1->2->3->4
13 min read
Sum of the nodes of a Circular Linked List
Given a singly Circular linked list. The task is to find the sum of nodes of the given linked list. For the above circular list, sum = 2 + 5 + 7 + 8 + 10 = 32 Examples: Input: 11->2->56->12 Output: Sum of Circular linked list is = 81 Input: 2-> 5 -> 7 -> 8 -> 10 Output: Sum of Circular linked list is = 32 Approach: Initialize a
7 min read
Delete all the even nodes of a Circular Linked List
Given a circular singly linked list containing N nodes, the task is to delete all the even nodes from the list. Examples: Input : 57->11->2->56->12->61 Output : List after deletion : 57 -> 11 -> 61 Input : 9->11->32->6->13->20 Output : List after deletion : 9 -> 11 -> 13 The idea is to traverse the nodes of
10 min read
Delete all Prime Nodes from a Circular Singly Linked List
Given a circular singly linked list containing N nodes. The task is to delete all nodes from the list which are prime. Examples: Input : 9->11->32->6->13->20 Output : 9 32 6 20 Input : 6->11->16->21->17->10 Output : 6 16 21 10 Approach: The idea is to traverse the nodes of the circular singly linked list one by one and
13 min read
Find minimum and maximum elements in singly Circular Linked List
Given a singly circular linked list of [Tex]N [/Tex]nodes. The task is to find the smallest and largest elements in the circular linked list. Examples: Input : List = 99->11->22->33->44->55->66 Output : Minimum = 11, Maximum = 99 Input : List = 12->11->9->33->125->6->99 Output : Minimum = 6, Maximum = 125 The ide
14 min read
Delete all odd or even positioned nodes from Circular Linked List
Delete all odd or even position nodes from circular linked list Given a Singly Circular Linked List, starting from the first node delete all odd position nodes in it. Note: Linked list is considered to have 1-based indexing. That is, first element in the linked list is at position 1. Examples: Input : List = 99->11->22->33 Output : 11->
15+ min read
Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
Given a singly circular linked list. The task is to find the sum and product of nodes that are divisible by K of the given linked list. Examples: Input : List = 5->6->7->8->9->10->11->11 K = 11 Output : Sum = 22, Product = 121 Input : List = 15->7->3->9->11->5 K = 5 Output : Product = 75, Sum = 20 Approach: Initi
12 min read
Next Greater Element in a Circular Linked List
Given a circular singly linked list, the task is to print the next greater element for each node in the linked list. If there is no next greater element for any node, then print "-1" for that node. Examples: Input: head = 1 ? 5 ? 2 ? 10 ? 0 ? (head)Output: 5 10 10 -1 -1Explanation:The next greater elements of each node are: Node 1: Next greater ele
11 min read
Splitting starting N nodes into new Circular Linked List while preserving the old nodes
Given a circular linked list with N nodes and an integer K where 0 < K < N, the task is to split the first K nodes into a new list and at the same time preserving the rest of the nodes in the original circular linked list. Examples: Input: 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8, K = 3 Output: Original list: 2 3 4 5 6 7 8 The new li
13 min read
Insertion in a sorted circular linked list when a random pointer is given
Given an array arr[] of integers and a pointer to a random node of a circular sorted linked list (initially empty), the task is to insert all the elements of arr[] into the circular linked list. Examples: Input: arr[] = {12, 56, 2, 11, 1, 90} Output: 1 2 11 12 56 90 Input: arr[] = {6, 2, 78, 45, 200} Output: 2 6 45 78 200 Approach: We are given a r
12 min read
Delete all odd nodes of a Circular Linked List
Prerequisite: Delete all the even nodes of a Circular Linked ListGiven a circular singly linked list containing N nodes, the task is to delete all the odd nodes from the list. Examples: Input: 572->112->21->5->1->6 Output: 572 -> 112 -> 6 Explanation: All the odd valued nodes have been deletedInput: 9->11->32->6->13
10 min read
Remove all Fibonacci Nodes from a Circular Singly Linked List
Given a circular singly linked list containing N nodes, the task is to remove all the nodes from the list which contains Fibonacci data values.Examples: Input: CLL = 9 -> 11 -> 34 -> 6 -> 13 -> 20 Output: 9 -> 11 -> 6 -> 20 Explanation: The list contains 2 fibonacci data values 32 and 13. Therefore, the nodes containing this
15+ min read