Circular Linked List
Circular Linked List
(/)
(https://freestar.com/?
_campaign=branding&utm_medium=banner&utm_source=baeldung.com&utm
ntent=baeldung_leaderboard_atf)
by baeldung (https://www.baeldung.com/author/baeldung/)
https://www.baeldung.com/java-circular-linked-list 1/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
1. Introduction
In this tutorial, we'll look at the implementation of a circular linked list in
Java.
https://www.baeldung.com/java-circular-linked-list 2/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
(https://freestar.com/?
3. Implementation in Java
Let's start by creating an auxiliary Node class that will store int values and a
pointer to the next node:
class Node {
int value;
Node nextNode;
Now let's create the first and last nodes in the circular linked list, usually
called the head and tail:
// ....
}
https://www.baeldung.com/java-circular-linked-list 3/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
In the next subsections we'll take a look at the most common operations we
can perform on a circular linked list.
https://www.baeldung.com/java-circular-linked-list 4/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
if (head == null) {
head = newNode;
} else {
tail.nextNode = newNode;
}
tail = newNode;
tail.nextNode = head;
}
cll.addNode(13);
cll.addNode(7);
cll.addNode(24);
cll.addNode(1);
cll.addNode(8);
cll.addNode(37);
cll.addNode(46);
return cll;
}
https://www.baeldung.com/java-circular-linked-list 5/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
(https://freestar.com/?
if (head == null) {
return false;
} else {
do {
if (currentNode.value == searchValue) {
return true;
}
currentNode = currentNode.nextNode;
} while (currentNode != head);
return false;
}
}
Now, let's add a couple of tests to verify that the above-created list contains
the elements we added and no new ones:
https://www.baeldung.com/java-circular-linked-list 6/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
@Test
public void
givenACircularLinkedList_WhenAddingElements_ThenListContainsThoseElements() {
CircularLinkedList cll = createCircularLinkedList();
assertTrue(cll.containsNode(8));
assertTrue(cll.containsNode(37));
}
@Test
public void
givenACircularLinkedList_WhenLookingForNonExistingElement_ThenReturnsFalse()
{
CircularLinkedList cll = createCircularLinkedList();
assertFalse(cll.containsNode(11));
}
https://www.baeldung.com/java-circular-linked-list 7/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
Let's now create some tests to verify that deletion works as expected for all
the cases:
https://www.baeldung.com/java-circular-linked-list 8/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
@Test
public void
givenACircularLinkedList_WhenDeletingInOrderHeadMiddleTail_ThenListDoesNotCon
tainThoseElements() {
CircularLinkedList cll = createCircularLinkedList();
assertTrue(cll.containsNode(13));
cll.deleteNode(13);
assertFalse(cll.containsNode(13));
assertTrue(cll.containsNode(1));
cll.deleteNode(1);
assertFalse(cll.containsNode(1));
assertTrue(cll.containsNode(46));
cll.deleteNode(46);
assertFalse(cll.containsNode(46));
}
@Test
public void
givenACircularLinkedList_WhenDeletingInOrderTailMiddleHead_ThenListDoesNotCon
tainThoseElements() {
CircularLinkedList cll = createCircularLinkedList();
assertTrue(cll.containsNode(46));
cll.deleteNode(46);
assertFalse(cll.containsNode(46));
assertTrue(cll.containsNode(1));
cll.deleteNode(1);
assertFalse(cll.containsNode(1));
assertTrue(cll.containsNode(13));
cll.deleteNode(13);
assertFalse(cll.containsNode(13));
}
@Test
public void
givenACircularLinkedListWithOneNode_WhenDeletingElement_ThenListDoesNotContai
nTheElement() {
CircularLinkedList cll = new CircularLinkedList();
cll.addNode(1);
cll.deleteNode(1);
assertFalse(cll.containsNode(1));
}
https://www.baeldung.com/java-circular-linked-list 9/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
if (head != null) {
do {
logger.info(currentNode.value + " ");
currentNode = currentNode.nextNode;
} while (currentNode != head);
}
}
As we can see, in the above example, during the traversal, we simply print
the value of each of the nodes, until we get back to the head node.
4. Conclusion
https://www.baeldung.com/java-circular-linked-list 10/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
In this tutorial, we've seen how to implement a circular linked list in Java and
explored some of the most common operations.
First, we learned what exactly a circular linked list is including some of the
most common features and differences with a conventional linked list. Then,
we saw how to insert, search, delete and traverse items in our circular linked
list implementation.
As usual, all the examples used in this article are available over on GitHub.
(https://github.com/eugenp/tutorials/tree/master/data-structures)
https://www.baeldung.com/java-circular-linked-list 11/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
COURSES
ALL COURSES (/ALL-COURSES)
ALL BULK COURSES (/ALL-BULK-COURSES)
THE COURSES PLATFORM (HTTPS://COURSES.BAELD UNG.COM)
SERIES
JAVA “BACK TO BASICS” TUTORIAL (/JAVA-TUTORIAL)
JACK SON JSON TUTORIAL (/JACK SON)
APACHE HTTPCLIENT TUTORIAL (/ HTTPCLIENT-GUID E)
REST W ITH SPRING TUTORIAL (/ REST-W ITH-SPRING-SERIES)
SPRING PERSISTENCE TUTORIAL (/ PERSISTENCE-W ITH-SPRING-SERIES)
SECURITY W ITH SPRING (/ SECURITY-SPRING)
SPRING REACTIVE TUTORIALS (/ SPRING-REACTIVE-GUID E)
ABOUT
ABOUT BAELD UNG (/ABOUT)
https://www.baeldung.com/java-circular-linked-list 12/13
9/12/22, 3:01 AM Circular Linked List Java Implementation | Baeldung
https://www.baeldung.com/java-circular-linked-list 13/13