how to go the middle of the singularly linked list in one iteration?
Posted
by
u3050
on Stack Overflow
See other posts from Stack Overflow
or by u3050
Published on 2010-12-22T12:36:45Z
Indexed on
2010/12/22
12:54 UTC
Read the original article
Hit count: 200
java
|data-structures
Recently I have been asked one question that in a singularly linked list how do we go to the middle of the list in one iteration.
A --> B --> C --> D (even nodes)
for this it should return address which points to B
A --> B --> C (odd nodes)
for this also it should return address which points to B
There is one solution of taking two pointers one moves one time and other moves two times but it does not seem working 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 anyone has did this before.
© Stack Overflow or respective owner