Check whether the length of given linked list is Even or Odd | Java program to check length of a given linklist ; Defining structure ; Function to check the length of linklist ; Push function ; Allocating node ; Info into node ; Next of new node to head ; head points to new node ; Driver code ; Adding elements to Linked List ; Checking for length of linklist
import java . io . * ; class GfG { static class Node { int data ; Node next ; } static int LinkedListLength ( Node head ) { while ( head != null && head . next != null ) { head = head . next . next ; } if ( head == null ) return 0 ; return 1 ; } static void push ( Node head , int info ) { Node node = new Node ( ) ; node . data = info ; node . next = ( head ) ; ( head ) = node ; } public static void main ( String [ ] args ) { Node head = null ; push ( head , 4 ) ; push ( head , 5 ) ; push ( head , 7 ) ; push ( head , 2 ) ; push ( head , 9 ) ; push ( head , 6 ) ; push ( head , 1 ) ; push ( head , 2 ) ; push ( head , 0 ) ; push ( head , 5 ) ; push ( head , 5 ) ; int check = LinkedListLength ( head ) ; if ( check == 0 ) { System . out . println ( " Odd " ) ; } else { System . out . println ( " Even " ) ; } } }