Linked List – peek, peekFirst, and peekLast methods

In this post, we will discuss the peek(), peekFirst(), and peekLast() methods of the Java LinkedList class. These methods retrieve the element present at the start or the end of the linked list.

public E peek()

peek() method is introduced in Java 5.

  • What does it do? It retrieves the linked list’s first node ( or head node ) value. Note: It will not remove the head of the linked list.
  • What does it return? This function will return the head of the linked list; if the linked list is empty, it will return null.
import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {

		LinkedList<Object> linkedList = new LinkedList<Object>();

		linkedList.add("codekru");
		linkedList.add("learns");
		linkedList.add("with");
		linkedList.add("you");

		System.out.println("Head of the linked list is: " + linkedList.peek());

		LinkedList<Object> emptyLinkedList = new LinkedList<Object>();
		System.out.println("Head of the empty linked list is: " + emptyLinkedList.peek());

	}
}

Output –

Head of the linked list is: codekru
Head of the empty linked list is: null
Internal implementation of the peek() method
    public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }

We can see from the internal implementation that the peek() method will return the head node value if present; otherwise, it will return null.

Time complexity of the peek() method

The time complexity of the peek() method would be O(1) as it’s returning the head node value only.

public E peekFirst()

peekFirst() was introduced in Java 6.

  • What does it do? It also retrieves the first element ( or head) from the linked list.
  • What does it return? This function will also return the head of the linked list; if the linked list is empty, it will return null.
import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {

		LinkedList<Object> linkedList = new LinkedList<Object>();

		linkedList.add("codekru");
		linkedList.add("learns");
		linkedList.add("with");
		linkedList.add("you");

		System.out.println("Head of the linked list is: " + linkedList.peekFirst());

		LinkedList<Object> emptyLinkedList = new LinkedList<Object>();
		System.out.println("Head of the empty linked list is: " + emptyLinkedList.peekFirst());

	}
}

Output –

Head of the linked list is: codekru
Head of the empty linked list is: null
Internal implementation of the peekFirst() method
    public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }
Time complexity of the peekFirst() method

The time complexity of the peekFirst() method is also O(1).

public E peekLast()

peekLast() method was also introduced in Java 6.

  • What does it do? It will retrieve the last element present in the linked list.
  • What does it return? It will return the last element of the linked list, and if the linked list is empty, it will return null.
import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {

		LinkedList<Object> linkedList = new LinkedList<Object>();

		linkedList.add("codekru");
		linkedList.add("learns");
		linkedList.add("with");
		linkedList.add("you");

		System.out.println("Last element of the linked list is: " + linkedList.peekLast());

		LinkedList<Object> emptyLinkedList = new LinkedList<Object>();
		System.out.println("Last element of the empty linked list is: " + emptyLinkedList.peekLast());

	}
}

Output-

Last element of the linked list is: you
Last element of the empty linked list is: null
Internal implementation of the peekLast() method
    public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }
Time complexity of the peekLast() method

The Time complexity of the peekLast() method is also O(1). The Java linked list also keeps track of the last node, and peekLast() returns the value present at the last node.

Difference between peek() vs. peekFirst() methods

Now, a question must be popping into your mind that peek() and peekFirst() methods are doing the same thing. So why are there two functions in the Linked List class?

peek() method was introduced in java 1.5 under the queue interface, while peekFirst() was introduced in java 1.6 under the deque interface. And as the linked list class implements both the interfaces, it has to implement both the methods and ends up implementing two functions doing essentially the same thing.

Using peek(), peekFirst() and peekLast() in a single program

Until now, you have got the idea of how all the peek methods work. So, now, let’s use them in a single program.

import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {

		LinkedList<Object> linkedList = new LinkedList<Object>();

		linkedList.add("codekru");
		linkedList.add("learns");
		linkedList.add("with");
		linkedList.add("you");

		System.out.println("peek() method returned: " + linkedList.peek());
		System.out.println("peekFirst() method returned: " + linkedList.peekFirst());
		System.out.println("peekLast() method returned: " + linkedList.peekLast());

	}
}

Output –

peek() method returned: codekru
peekFirst() method returned: codekru
peekLast() method returned: you

We recommend reading this article to learn more about the linked list and its methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *