In this post, We will learn How to print circular linked list in java
This problem is the extension of How to check whether a linked list has a loop/cycle in java ?
Logic is very simple to print Circular LinkedList:
Here traversal logic can be performed by simply iterating each node of the LinkedList starting from the head node Until we encounter the head node again.
Complete Source is given Below:
Node.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.kkjavatutorials.util; /** * This class represent Node in Linked List * @author KK JavaTutorials */ public class Node<T>{ //Data hold by LinkedList Node private T data; //Reference to point next Node private Node<T> next; public Node(T data) { super(); this.data = data; this.next = null; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Node<T> getNext() { return next; } public void setNext(Node<T> next) { this.next = next; } } |
LinkedList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.kkjavatutorials.util; /** * Print Circular List List * @author KK JavaTutorials */ public class LinkedList<T> { //head always point to first node private Node<T> head; /** * Method to insert Node in Linked List * @param newNode insert to List */ public void insert(Node<T> newNode) { if(isEmpty()) { head = newNode; }else { Node<T> current = head; while(current.getNext() !=null) { current = current.getNext(); } current.setNext(newNode); } } //Check if Linked is empty private boolean isEmpty() { return head == null; } /** * Print Circular List List * Here traversal logic can be performed by simply * iterating each node of the LinkedList starting from the head node * Until we encounter the head node again */ public void printCircularLinkedList() { if (head != null){ Node<T> currentNode = head; while (currentNode.getNext() != head){ System.out.print(currentNode.getData() + " "); currentNode = currentNode.getNext(); } System.out.print(currentNode.getData()); } } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.kkjavatutorials.util; /** * @author KK JavaTutorials * Client program to print circular linked list */ public class ClientTest { public static void main(String[] args) { //Create LinkedList Instance LinkedList<Integer> linkedList = new LinkedList<>(); Node<Integer> node1 = new Node<Integer>(2); linkedList.insert(node1); linkedList.insert(new Node<Integer>(5)); linkedList.insert(new Node<Integer>(8)); linkedList.insert(new Node<Integer>(40)); linkedList.insert(new Node<Integer>(10)); linkedList.insert(new Node<Integer>(90)); linkedList.insert(node1); System.out.println("Print Circular LinkedList::"); linkedList.printCircularLinkedList(); } } |
Output Of this Program:
Print Circular LinkedList::
2 5 8 40 10 90
You May Also Like:
Implementing a Circular Singly Linked List in Java ?
How to check whether a linked list has a loop/cycle in java ?
Find start node of loop in Singly LinkedList in java
Count the number of nodes in a circular linked list?
That’s all about the How to print a Circular linked list in java?
If you have any feedback or suggestion please feel free to drop in below comment box.