In this post, We will learn How will you print a LinkedList from the End?
Logic Used In Blow Source Code:
We have to traverse recursively till the end of the LinkedList and While coming back, Start printing every element. That’s Yet.
Below Source code shown output as below:
Display LinkedList from Start to End:::
10 20 30 50 60 70
Display LinkedList from End to Start:::
70 60 50 30 20 10
From the above output, it is clear that When we print LinkedList from End to Start then output is the exact reverse of when we print LinkedList from Start to End.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * This method print LinkedList from End * @param head of List */ public void printLinkedListFromHead(Node<T> head) { if(head == null) { return; } printLinkedListFromHead(head.getNext()); System.out.print(head.getData()+" "); } |
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 51 52 53 54 55 56 57 58 59 60 61 |
package com.kkjavatutorials.util; /** * Singly LinkedList Implementation * @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; } /** * Method which traverse Linked List and display all data. */ public void displayLinkedList() { Node<T> currentNode = head; while(currentNode!= null) { System.out.print(currentNode.getData() +" "); currentNode = currentNode.getNext(); } } /** * This method print LinkedList from End * @param head of List */ public void printLinkedListFromHead(Node<T> head) { if(head == null) { return; } printLinkedListFromHead(head.getNext()); System.out.print(head.getData()+" "); } public Node<T> gethead(){ return head; } } |
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 Display LinkedList from End to Start */ public class ClientTest { public static void main(String[] args) { //Create LinkedList Instance LinkedList<Integer> linkedList = new LinkedList<>(); linkedList.insert(new Node<Integer>(10)); linkedList.insert(new Node<Integer>(20)); linkedList.insert(new Node<Integer>(30)); linkedList.insert(new Node<Integer>(50)); linkedList.insert(new Node<Integer>(60)); linkedList.insert(new Node<Integer>(70)); System.out.println("Display LinkedList from Start to End:::"); linkedList.displayLinkedList(); System.out.println(); System.out.println("Display LinkedList from End to Start:::"); linkedList.printLinkedListFromHead(linkedList.gethead()); } } |
Output Of this Program:
Display LinkedList from Start to End:::
10 20 30 50 60 70
Display LinkedList from End to Start:::
70 60 50 30 20 10
Time Complexity: O(n) and Space Complexity: O(n) for method Stack
You May Also Like:
How to remove a given key from the Singly Linked List in Java ?
How to find the middle node in a Singly Linked List in Java ?
How to search an element in a Singly Linked List in Java ?
How to find the length of a Singly Linked List in Java?
Implementation to reverse a Singly Linked List in Java ?
How to check if LinkedList is palindrome or not in java ?
How to convert sorted Linked List to balanced Binary Search Tree?
How to find the intersection of two LinkedList in java
How to find Nth node from the end of a Singly Linked List in Java?
That’s all about the How will you print a LinkedList from the End?
If you have any feedback or suggestion please feel free to drop in below comment box.