In this post, We will learn How to write a java program to Check Whether the given LinkedList length is even or odd?
Logic Used In Blow Source Code:
We have to use a 2X pointer means to take a pointer that moves two nodes at a time, in the end, if the length is even then the pointer will be null otherwise it will point to the last node.
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 62 63 64 65 66 67 68 69 |
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(); } } /** * Method to check whether LinkedList * Length is Odd or Even * @return true if LinkedList Length * is Odd else return false(means Length is Even) */ public boolean isLinkedListLengthOdd() { //Length is Odd boolean isOddLength = true; if(head == null) { //Length is Even isOddLength = false; return isOddLength; } while(head !=null && head.getNext() != null) { head = head.getNext().getNext(); } if(head == null) { //Length is Even isOddLength = false; return isOddLength; } return isOddLength; } } |
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 29 30 31 |
package com.kkjavatutorials.util; /** * @author KK JavaTutorials * Client program to check whether LinkedList Length is Odd or Even */ public class ClientTest { public static void main(String[] args) { //Create LinkedList Instance LinkedList<Integer> linkedList1 = new LinkedList<>(); linkedList1.insert(new Node<Integer>(10)); linkedList1.insert(new Node<Integer>(20)); linkedList1.insert(new Node<Integer>(30)); linkedList1.insert(new Node<Integer>(50)); linkedList1.insert(new Node<Integer>(60)); linkedList1.insert(new Node<Integer>(70)); System.out.println("Displaying LinkedList:"); linkedList1.displayLinkedList(); System.out.println(); if(linkedList1.isLinkedListLengthOdd()) { System.out.println("LinkedList Length is Odd"); }else { System.out.println("LinkedList Length is Even"); } } } |
Output Of this Program:
Displaying LinkedList:
10 20 30 50 60 70
LinkedList Length is Even
Time Complexity: O[n/2] and Space Complexity: O(1) to hold boolean value(isOddLength)
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 Check Whether the given LinkedList length is even or odd?
If you have any feedback or suggestion please feel free to drop in below comment box.