Print Linked List in Java

Print Linked List in Java

A linked list is used to store elements which are called nodes. Nodes comprise two entities - the data and a reference. Reference points to the next node. Their address in the memory does not define the order of the elements of a linked list.

The Java programming language provides us with the LinkedList class. This class is available under Java’s Collections Framework, which provides the functionality of this doubly linked list data structure.

In this tutorial, we will introduce how to print a linked list in Java.

We can create a linked list object using the LinkedList class, add some elements using the add() function, and display it.

See the code below.

import java.util.*;

public class ABC{
    public static void main(String []args){
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("apple");
        fruits.add("orange");
        fruits.add("mango");
        String str = fruits.listIterator(1).previous();
        System.out.println(fruits);
    }
}

Output:

[apple, orange, mango]

We can also use the toString() function to display the final list as a string. The toString() method returns the linked list elements in a string format separated using commas.

For example,

import java.util.*;

public class ABC{
    public static void main(String []args){
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("apple");
        fruits.add("orange");
        fruits.add("mango");
        String str = fruits.listIterator(1).previous();
        System.out.println(fruits.toString());
    }
}

Output:

[apple, orange, mango]

This method parses the linked list from start to end and separates them using commas.

We can also create a user-defined class to initiate a linked list and create the necessary functionalities. While working with such a class, we will need to create a method to display the list.

We will need to override the toString() function while using this method and updating it based on the user’s requirement.

For example,

public class LinkedListNode {
    private int data;
    private LinkedListNode next;
    public LinkedListNode(int data) {
        this.data = data;
        this.next = null;
    }
    public int getData() {
        return data;
    }
    public void setData(int d) {
        data = d;
    }
    public LinkedListNode getNext() {
        return next;
    }
    public void setNext(LinkedListNode n) {
        next = n;
    }
}

public class LinkedList {
    public LinkedListNode head;
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insertFront(1);
        list.insertFront(2);
        list.insertFront(3);
        System.out.println(list.toString());
    }
    public LinkedList() {
        this.head = null;
    }
    public int removeFront(){
        if(head == null){
            System.out.println("Empty list");
            return 0;
        }else{
            int temp = head.getData();
            head = head.getNext();
            return temp;
        }
    }
    public void insertFront(int data){
        if(head == null){
            head = new LinkedListNode(data);
        } else {
            LinkedListNode newNode = new LinkedListNode(data);
            newNode.setNext(head);
            head = newNode;
        }       
    }
    public void insertBack(int data){
        if(head == null){
            head = new LinkedListNode(data);
        }else{
            LinkedListNode newNode = new LinkedListNode(data);
            LinkedListNode current = head;
            while(current.getNext() != null){
                current = current.getNext();
            }
            current.setNext(newNode);
        }       
    }

    public int removeBack(){
        if(head == null){
            System.out.println("Empty list");
            return 0;
        } else if (head.getNext() == null) {
            int temp = head.getData();
            head = null;
            return temp;
        } else {
            LinkedListNode current = head;
            while(current.getNext().getNext() != null){
                current = current.getNext();
            }
            int temp = current.getNext().getData();
            current.setNext(null);
            return temp;
        }       
    }
    
    public String toString() {
        String result = "";
        LinkedListNode current = head;
        while(current.getNext() != null){
            result += current.getData();
            if(current.getNext() != null){
                result += ", ";
            }
            current = current.getNext();
        }
        result += current.getData();
        return "Contents of the List: " + result;
    }
    
    public LinkedListNode getHead() {
        return head;
    }
}

Output:

Contents of the List: 3, 2, 1

We can also directly print the required list without formatting it into a string. We also start with the initial node, display it, and then move on to the next node.

See the following code.

public class LinkedListNode {
    private int data;
    private LinkedListNode next;
    public LinkedListNode(int data) {
        this.data = data;
        this.next = null;
    }
    public int getData() {
        return data;
    }
    public void setData(int d) {
        data = d;
    }
    public LinkedListNode getNext() {
        return next;
    }
    public void setNext(LinkedListNode n) {
        next = n;
    }
}

public class LinkedList {
    public LinkedListNode head;
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insertFront(1);
        list.insertFront(2);
        list.insertFront(3);
        list.display();
    }
    public LinkedList() {
        this.head = null;
    }
    public int removeFront(){
        if(head == null){
            System.out.println("Empty list");
            return 0;
        }else{
            int temp = head.getData();
            head = head.getNext();  
            return temp;
        }
    }
    public void insertFront(int data){
        if(head == null){
            head = new LinkedListNode(data);
        } else {
            LinkedListNode newNode = new LinkedListNode(data);
            newNode.setNext(head);
            head = newNode;
        }       
    }
    public void insertBack(int data){
        if(head == null){
            head = new LinkedListNode(data);
        }else{
            LinkedListNode newNode = new LinkedListNode(data);
            LinkedListNode current = head;
            while(current.getNext() != null){
                current = current.getNext();
            }
            current.setNext(newNode);
        }       
    }

    public int removeBack(){
        if(head == null){
            System.out.println("Empty list");
            return 0;
        } else if (head.getNext() == null) {
            int temp = head.getData();
            head = null;
            return temp;
        } else {
            LinkedListNode current = head;
            while(current.getNext().getNext() != null){
                current = current.getNext();
            }
            int temp = current.getNext().getData();
            current.setNext(null);
            return temp;
        }       
    }
    
    public void display() {
        LinkedListNode current = head;
        while(current.getNext() != null){
            System.out.println(current.getData());
            current = current.getNext();
        }
        System.out.println(current.getData());
    }
    
    public LinkedListNode getHead() {
        return head;
    }
}

Output:

3
2
1

Related Article - Java Linked List

  • Remove a Node From a Linked List in Java
  • Array of Linked List in Java
  • Sort Linked List in Java
  • Doubly Linked List in Java