Table of Content


Understanding Linked List

  1. Introduction to Linked Lists:
  2. Comparison with Dynamic Arrays:
  3. Components of Linked Lists:
  4. Operations on Linked Lists:
  5. Advantages of Linked Lists:
  6. Drawbacks of Linked Lists:

Type of Linked List

  1. Singly LinkedList:
  2. Doubly LinkedList:
  3. Circular LinkedList:

Hands-on: LinkedList

  1. Creating a Custom LinkedList:
  2. Adding Nodes:
  3. Removing Nodes:

Code Example:

class LL {

   Node head;
   private int size;

   public LL() {
       size = 0;
   }

   public class Node {
       String data;
       Node next;

       Node(String data) {
           this.data = data;
           this.next = null;
       }
   }

   public void addFirst(String data) {
       Node node = new Node(data);
       node.next = head;
       head = node;
       size++;
   }

   public void addLast(String data) {
       Node node = new Node(data);
       if(head==null) {
           head = node;
           size++;
           return;
       }
       Node lastNode = head;
       while (lastNode.next != null) {
           lastNode = lastNode.next;
       }
       lastNode.next = node;
       size++;
   }

   public void printlist() {
       Node currNode = head;
       while (currNode != null) {
           System.out.print(currNode.data + " -> ");
           currNode = currNode.next;
       }
       System.out.println("null");
   }

   public void removeFirst() {
       if(head == null) {
           System.out.println("Empty List, nothing to delete");
           return;
       }
       head = this.head.next;
       size ++;
   }

   public void removeLast() {
       if(head == null) {
           System.out.println("Empty List, nothing to delete");
           return;
       }
       if(head.next == null) {
           head = null;
           size--;
           return;
       }
       Node currNode = head;
       Node lastNode = head.next;
       while(lastNode.next != null) {
           currNode = currNode.next;
           lastNode = lastNode.next;
       }
       currNode.next = null;
       size --;
   }

   public int getSize() {
       return this.size;
   }
}

Using Java's LinkedList Class: