1->2->3->4 == true
1->2->3->4->5 == false
Traversing over the linked list with 2x the speed (over two nodes at a time) in a loop can give you an interesting and efficient solution in a single pass.
1. Traverse through the List
by hopping two Nodes
at a time.
2. While traversing, keep a check to exit out of loop, if the next
reference of the current Node
becomes null
.
3. If the current Node
becomes null
at the end of List
traversal, it's length is even. Otherwise, it's odd.
public Boolean isListEven(ListNode head) { }
C
Java
Python