Pages

Saturday, September 13, 2025

Doubly Linked List in Java

public class DoubleLL {

    public class Node {
        int data;
        Node next;
        Node prev;

        public Node(int data) {
            this.data = data;
            this.next = null;
            this.prev = null;
        }
    }


    public static Node head;
    public static Node tail;
    public static int size;


    // add
    public void addFirst(int data) {
        Node newNode = new Node(data);
        size++;
        if(head == null) {
            head = tail = newNode;
            return;
        }


        newNode.next = head;
        head.prev = newNode;
        head = newNode;

    }

    // print
    public void print() {
        Node temp = head;
        while(temp != null) {
            System.out.print(temp.data + " <-> ");
            temp = temp.next;

        }
        System.out.println("null");
    }

    // remove - removeLast
    public int removeFirst() {
        if(head == null) {
            System.out.println("DLL Is empty");
            return Integer.MIN_VALUE; // prints minus infinity
        }

        if(size == 1) {
            int val = head.data;
            head = tail = null;
            size--;
            return val;
        }

        int val = head.data;
        head = head.next;
        head.prev = null; // line error incase if single Node
        size--;
        return val;

    }

    public void addLast(int data) {
        Node newNode = new Node(data);

        if(head == null || tail == null) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
        }
        size++;
    }

    public int removeLast() {
       
        // empty node case
        if(head == null) {
            System.out.println("DLL is empty");
            return Integer.MIN_VALUE;
        }

        // one-node case
        if(size == 1) {
            head = tail = null;
        } else {
            tail = tail.prev;
            tail.next = null;
        }
        return size--;

    }

    public static void main(String args[]) {
        DoubleLL dll = new DoubleLL();
        dll.addFirst(3);
        dll.addFirst(2);
        dll.addFirst(1);

        dll.print();
        System.out.println(dll.size); // 1<->2<->3<->null

        dll.removeFirst();
        dll.print();
        System.out.println(dll.size); // 2 <-> 3 <-> null

        dll.addLast(1);
        dll.print();
        System.out.println(dll.size); //2 <-> 3 <-> 1 <-> null

        dll.removeLast();
        dll.print();
        System.out.println(dll.size); //2 <-> 3 <-> null
    }
   
}
OUTPUT: 1 <-> 2 <-> 3 <-> null 3 2 <-> 3 <-> null 2 2 <-> 3 <-> 1 <-> null 3 2 <-> 3 <-> null 2

No comments:

Post a Comment

remove duplicates from sorted array - two pointer approach (leetcode)

  class Solution {     public int removeDuplicates ( int [] nums ) {         // base case: return if array have no el.         if ( nums...