public void findMiddleNode(ListNode head){
ListNode p,q;
Hashtable<Integer, ListNode> table = new Hashtable<Integer, ListNode>();
int i = 1;
int length = 0;
for(p = head; (q = p.getNext())!= null; p =q){
/**
* here first I am getitng next node then adding to hash Table just to
* avoid adding the address of head Node
*/
p = p.getNext();
table.put(i, p);
i++;
length++;
}
int midData = length/2;
//System.out.println("Hash table is:-"+table);
System.out.println("Middle element is:-"+table.get(midData).getData());
}
No comments:
Post a Comment