Recently I was asked one question, what is in the only linked list, how do we go to the middle of the list in one iteration.
A --> B --> C --> D (even nodes)
To do this, it must return an address that points to B
A --> B --> C (odd nodes)
for this, it should also return an address that points to B
There is one solution consisting of two pointers: one move once, and the other two times, but it does not work here.
LinkedList p1,p2;
while(p2.next != null)
{
p1 = p1.next;
p2 = p2.next.next;
}
System.out.print("middle of the node" + p1.data); //This does not give accurate result in odd and even
Please help if someone has done this before.
source
share