Monday, 12 May 2014

LinkedList : peek : Get the Head of the List

public E peek()
Retrieves, but does not remove, the head (first element) of this list.

import java.util.*;

class LinkedListPeek{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  /* Add Elements to myList */
  myList.offerLast(10);
  myList.offerLast(20);
  myList.offerLast(30);
  myList.offerLast(40);
  
  System.out.print("Head of myList is ");
  System.out.println(myList.peek());
 }
}

Output
Head of myList is 10


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment