Question: I'm trying to write a Java function that takes a singly linked list : a->b->a->b->a->b->a->null, and groups both even and odd nodes based on position
I'm trying to write a Java function that takes a singly linked list: a->b->a->b->a->b->a->null, and groups both even and odd nodes based on position (not value). The function should work in-place and return all even nodes first, followed by odd nodes: b->b->b->a->a->a->a->null. I currently have the following solution:

However, this produces the error: Cannot assign field "next" because "even" is null. Could you please help fix this? The funtion must make all even nodes first, followed by all odd nodes.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
