Related Articles
Java Program to Delete a Node From the Beginning of the Circular Linked List
• Last Updated : 15 Dec, 2020

In this article, we will learn about deleting a node from the beginning of a circular linked list. Consider the linked list as shown below. Example:

```Input : 5->3->4->(head node)
Output: 3->4->(head node)```

Two cases arrive while solving the problem,

Case 1: List is empty

• If the list is empty we will simply return.

Case 2: List is not empty

• Define a node class which represents the node.
• delete():
• return from the function if no node is present
• sets both head and tail to null if only one node is there
• if it has more than one node then it removes the previous head node, the head will point to the next node in the list and tail will point to the new head.
• printNode() will print all the nodes present in the list as:
• Node current is defined which will point to the head
• Print current.val till it starts pointing to the head again
• In each iteration, it will point to the next node

Code:

## Java

 `// Java Program to Delete a Node From the Beginning of the``// Circular Linked List`` ` `public` `class` `Main {`` ` `    ``// Represents the node of list.``    ``public` `class` `Node {``        ``int` `val;``        ``Node next;``        ``public` `Node(``int` `val) { ``this``.val = val; }``    ``}`` ` `    ``// Initialising head and tail pointers``    ``public` `Node head = ``null``;``    ``public` `Node tail = ``null``;`` ` `    ``// add new node to the end``    ``public` `void` `addNode(``int` `val)``    ``{``        ``// Creating new node``        ``Node node = ``new` `Node(val);`` ` `        ``// head and tail will point to new node``        ``// if list is empty``        ``if` `(head == ``null``) {``            ``head = node;``            ``tail = node;``            ``node.next = head;``        ``}`` ` `        ``// otherwise tail point to new node and``        ``else` `{``            ``tail.next = node;``            ``tail = node;``            ``tail.next = head;``        ``}``    ``}`` ` `    ``// Deletes node from the beginning of the list``    ``public` `void` `delete()``    ``{``        ``// returns if list is empty``        ``if` `(head == ``null``) {``            ``return``;``        ``}`` ` `        ``// otherwise head will point to next element in the``        ``// list and tail will point to new head``        ``else` `{``            ``if` `(head != tail) {``                ``head = head.next;``                ``tail.next = head;``            ``}`` ` `            ``// if the list contains only one element``            ``// then both head and tail will point to null``            ``else` `{``                ``head = tail = ``null``;``            ``}``        ``}``    ``}`` ` `    ``// displaying the nodes``    ``public` `void` `display()``    ``{``        ``Node current = head;``        ``if` `(head == ``null``) {``            ``System.out.println(``"List is empty"``);``        ``}``        ``else` `{``            ``do` `{``                ``System.out.print(``" "` `+ current.val);``                ``current = current.next;``            ``} ``while` `(current != head);``            ``System.out.println();``        ``}``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``Main list = ``new` `Main();`` ` `        ``// Adds data to the list``        ``list.addNode(``5``);``        ``list.addNode(``3``);``        ``list.addNode(``4``);`` ` `        ``// Printing original list``        ``System.out.println(``"Original List: "``);``        ``list.display();`` ` `        ``// deleting node from beginning and``        ``// displaying the Updated list``        ``list.delete();``        ``System.out.println(``"Updated List: "``);``        ``list.display();``    ``}``}`
Output
```Original List:
5 3 4
Updated List:
3 4```

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up