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.
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.
- 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
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.