• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Introduction » How to Implementing linked lists in java
Next →
← Prev

How to Implementing linked lists in java

By Dinesh Thakur

In this section, we will see the implementation of a simplified version of linked list class provided by the Java library. This shows how the lists of operations manipulate the links when the list is modified.

To keep this simple example code does not implement all methods of the class linked list. We will implement only one simply linked list, and the class list will provide direct access only to the first element of the list, not the last. Our list will not use a type parameter. We will simply store raw Object values and use coercions to get them back. The result will be a fully functional class list that shows how the links are updated in the operations add and remove and how the iterator through the list.

An object can store an object and a reference to the next node. Because the methods of both the class linked list as the iterator class have frequent access to the Node instance variables do not become private instance variables. Instead, we will make a private inner class Node of the LinkedList class. As none of the list of methods returns a Node object is safe to leave the public instance variables.

public class LinkedList
{
     . . .
     private class Node
     {
         public Object data;
         public Node next;
     }
}

The LinkedList class stores a reference first to the first node (or null if the list is completely empty).

public class LinkedList
{
   public LinkedList()
   {
       first = null;
   }
   public Object getFirst()
   {
         if (first == null)
           throw new NoSuchElementException();
           return first.data;
   }
         . . .
         private Node first;
}

       Adding a node at the beginning of a linked list

Now let’s go back to the add First method (see Figure). When a new node is added to the list, it becomes the top of the list and the node that was the old top of the list; it becomes your next node:

public class LinkedList
{
   . . .
   public void addFirst(Object element)
   {
       Node newNode = new Node();
       newNode.data = element;
       newNode.next = first;
       first = newNode;
   }
       . . .
}

Remove the first element of the list works as follows. Data from the first node are saved and are later returned as the result of the method. The successor to the first node becomes the first node of the shortest list (see Figure). So no more references to the old node and the garbage collector will eventually recycle it.

public class LinkedList
{
     . . .
     public Object removeFirst()
     {
         if (first == null)
               throw new NoSuchElementException();
               Object element = first.data;
               first = first.next;
               return element;
     }
         . . .
}

          Removing the first node of a linked list

Now, back to the iterator class. The ListIterator interface in the standard library defines nine methods. We omit four of them (the methods kicking the iterator and the methods applied to an entire index of iterator).

Our LinkedList class defines an internal and private LinkedList iterator class that implements the simplified ListIterator interface. As LinkedListIterator is an inner class, it has access to private resources LinkedList class – in particular, the first field and the private class Node.

Note that clients of the LinkedList class, in fact, do not know the name of the iterator class. They just know it’s a class that implements the interface ListIterator.

public class LinkedList
{
   . . .
   public ListIterator listIterator()
   {
       return new LinkedListIterator();
   }
   private class LinkedListIterator
       implements ListIterator
       {
             public LinkedListIterator()
             {
                 position = null;
                 previous = null;
             }
                 . . .
             private Node position;
             private Node previous;
       }
         . . .
}

Each iterator object has a positive reference to the last node visited. We also store a reference to the last node before that. We will need that reference to adjust the links properly the remove method.

The next method is simple. The position reference is advanced to position.next and the old position is remembered in previous. But there is a special case – if the iterator point before the first element of the list, then the old position is null and position must be set to first.

private class LinkedListIterator implements ListIterator
{
   . . .
   public Object next()
   {
       if (!hasNext())
           throw new NoSuchElementException();
           previous = position;
           if (position == null)
             position = first;
           else
               position = position.next;
               return position.data;
     }
       . . .
}

The next method should only be called when the iterator is not at the end of the list. The iterator is at the end if the list is empty (i.e., first == null) or if there is no element after the current position (position.next == null).

private class LinkedListIterator implements ListIterator
{
   . . .
   public boolean hasNext()
   {
       if (position == null)
         return first != null;
       else
       return position.next != null;
   }
       . . .
}

Remove the last node visited is more complex. If the element to be removed is the first element, we call only remove- First. Otherwise, an element in the middle of the list must be removed and the previous node must have its next updated reference to skip the removed element (see Figure). If the reference is equal to the previous position, then this call to remove does not immediately follow a call to next and launched an IllegalStateException.

According to the method definition removes invalid call removes twice the line. Therefore, the remove method sets the reference position as previous.

private class LinkedListIterator implements ListIterator
{
   . . .
   public void remove()
   {
     if (previous == position)
         throw new IllegalStateException();
       if (position == first)
         {
             removeFirst();
         }
       else
         {
           previous.next = position.next;
         }
           position = previous;
   }
       . . .
}

The set method to change the data storage on previously visited element. Its implementation is simple and straightforward because our linked list can be traversed only in one direction. The implementation of standard library linked list should monitor whether the last movement of the iterator moved forward or backward. For this reason, the standard library prohibits a call to the set method following a method add or remove. We do not impose this restriction.

public void set(Object element)
{
   if (position == null)
       throw new NoSuchElementException();
       position.data = element;
}

       Removing a node through a linked list.

          Adding a node in the middle of a linked list

Finally, more complex operation is the addition of a node. You insert the new node after the current position and sets the successor of the new node as successor to the current position (see Figure)

private class LinkedListIterator implements ListIterator
{
   . . .
     public void add(Object element)
     {
         if (position == null)
           {
               addFirst(element);
               position = first;
           }
         else
           {
               Node newNode = new Node();
               newNode.data = element;
               newNode.next = position.next;
               position.next = newNode;
               position = newNode;
           }
               previous = position;
     }
         . . .
}

Full implementation of our LinkedList class is at the end of this section.

Now you know how to use the Java Library LinkedList class and had a quick introduction to understand how linked lists are implemented.

import java.util.NoSuchElementException; 
    public class LinkedList 
 { 
      public LinkedList() 
      { 
          first = null; 
      } 
      public Object getFirst() 
      { 
            if (first == null) 
                throw new NoSuchElementException(); 
                return first.data; 
      } 
      public Object removeFirst() 
        { 
          if (first == null) 
              throw new NoSuchElementException(); 
            Object element = first.data; 
              first = first.next; 
              return element; 
        } 
      public void addFirst(Object element) 
        { 
            Node newNode = new Node(); 
            newNode.data = element; 
            newNode.next = first; 
            first = newNode; 
        } 
        private Node first; 
        private class Node 
        { 
              public Object data; 
              public Node next; 
        } 
        private class LinkedListIterator implements ListIterator 
        { 
            public LinkedListIterator() 
            { 
              position = null; 
              previous = null; 
            } 
        public Object next() 
            { 
                if (!hasNext()) 
                  throw new NoSuchElementException(); 
                  previous = position; 
                  if (position == null) 
                    position = first; 
                  else 
                    position = position.next; 
                    return position.data; 
          } 
        public boolean hasNext() 
          { 
              if (position == null) 
                return first != null; 
                else 
                return position.next != null; 
          } 
        public void add(Object element) 
        { 
              if (position == null) 
                { 
                  addFirst(element); 
                  position = first; 
               } 
              else 
                { 
                    Node newNode = new Node(); 
                    newNode.data = element; 
                    newNode.next = position.next; 
                    position.next = newNode; 
                    position = newNode; 
                } 
                    previous = position; 
        } 
        public void remove() 
        { 
            if (previous == position) 
                throw new IllegalStateException(); 
              if (position == first) 
                  { 
                      removeFirst(); 
                  } 
              else 
                  { 
                      previous.next = position.next; 
                  } 
                      position = previous; 
        } 
        public void set(Object element) 
        { 
              if (position == null) 
                throw new NoSuchElementException(); 
                position.data = element; 
        } 
                private Node position; 
                private Node previous; 
    } 
 } 
 public interface ListIterator 
 { 
    Object next(); 
    boolean hasNext(); 
    void add(Object element); 
    void remove(); 
    void set(Object element); 
 }

You’ll also like:

  1. What is Linked Lists
  2. What is Circular Linked Lists
  3. What is Linked lists? How Many Type of Link List
  4. HTML UL – Unordered lists – Bulleted lists
  5. Implementing Inheritance in Java Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW