Linked Lists Operations

Before we can use any linked list operations, we need to initialize the data structure and mark it as empty. Conceptually, this is when the head of the list is pointing to nothing. We can do this in Java by adding this logic in a constructor.

The following code snippet shows this. Notice that, once again, we use generics to hold the type of the items we want to store in the linked list:

public class LinkedList<V> {  private LinkedListNode<V> head;  public LinkedList() {    head = null;  }}  
Snippet 2.15: Initializing the linked list data structure using constructors. Source class name: Linkedlist
Go to https://goo.gl/vxpkRt to access the code.

How can we add and remove items from the head of the list? Adding a node in a ...

Get Beginning Java Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.