Showing posts with label Find Middle element in linked list. Show all posts
Showing posts with label Find Middle element in linked list. Show all posts

Saturday, August 18, 2012

How to find middle element of linked list using java


import java.util.LinkedList;

public class MiddleElementLinkedList {

public static void main(String args[]){
LinkedList l1= new LinkedList();
l1.add(2);
l1.add(3);
l1.add(1);
l1.add(6);
l1.add(8);
System.out.println("Middle Element "+middleElementTwo(l1));

System.out.println("Loop: Middle Element "+middleElementLoop(l1));
}

private static String middleElementLoop(LinkedList l1) {
int end=l1.size()-1;
int start=0;

while(start >end){
start++;
end--;
}
if(start==end)
return (String) l1.get(start);
else
return (String) l1.get(start-1);
}

private static String middleElementTwo(LinkedList l1) {
int mid=(l1.size())/2;
return(l1.get(mid).toString());
}


}