Size of LinkedList

Linkedlist size we can implement by increasing the counter with traverse the loop.

We can get the size of linkedlist in 2 ways.

  1. Iterative
  2. Recursive

Iterative:

  1. Create a method name size which returns integer as linkedlist size.
  2. Create counter variable and set it to 0 (zero).
  3. Loop the list until last node is null.
  4. return the counter.
private static int size() {
    int counter = 0;
    Node temp = head;
    while (temp != null){
        counter++;
        temp = temp.next;
    }
    return counter;
}

Recursive:

  1. Create a method name size which returns integer as linkedlist size
  2. Create a method which receives head as argument and returns integer
  3. call 2nd method from the first method and return the integer value.
private static int getSize() {

    return getCount(head);
}

private static int getCount(Node head) {
    if(head == null){
        return 0;
    }
    return 1+getCount(head.next);
}

Full Program:

package com.gudla.linkedlist;

/**
 * Created by santhosh on 19/3/17.
 */
public class LinkedList {
    static Node head;

    static class Node{
        String data;
        Node next;
        Node(String string){
            data = string;
            next = null;
        }
    }

    public static void main(String[] args) {
//        LinkedList linkedList = new LinkedList();
        head = new Node("Santhosh");
        Node first = new Node("Muni");
        Node second = new Node("Basha");

//        Assign address of first node to head and second node to first
        head.next = first;
        first.next = second;

        insert("Vamsi");
        insert(first, "Hareesh");
        addAtEnd("Santa");

        delete("Hareesh");
        deleteAtPosition(2);

        System.out.println(size());
        System.out.println(getSize());

        printData();


    }

    private static int getSize() {

        return getCount(head);
    }

    private static int getCount(Node head) {
        if(head == null){
            return 0;
        }
        return 1+getCount(head.next);
    }

    private static int size() {
        int counter = 0;
        Node temp = head;
        while (temp != null){
            counter++;
            temp = temp.next;
        }
        return counter;
    }


    private static void deleteAtPosition(int position) {
        if(head == null){
            System.out.println("List is empty");
            return;
        }
        if(position == 0){
            head = head.next;
            return;
        }
        Node temp = head;
        for(int i = 0; temp != null && i < position-1; i++){
            temp = temp.next;
        }
        if(temp == null || temp.next == null){
            System.out.println("Given position is exceeded with list position");
            return;
        }
        temp.next = temp.next.next;
    }

    private static void delete(String data) {
        if(head != null && head.data == data){
            head = head.next;
            return;
        }
        Node temp, previous = null;
        temp = head;
        while( temp != null && temp.data != data){
            previous = temp;
            temp = temp.next;
        }
        if(temp == null){
            System.out.println("Given data is not found in the list");
            return;
        }
        previous.next = temp.next;
    }

    private static void addAtEnd(String data) {
        if(head == null ){
            head = new Node(data);
            return;
        }
        Node new_node = new Node(data);
        new_node.next = null;

        Node last = head;
        while (last.next != null){
            last = last.next;
        }
        last.next = new_node;
    }

    private static void insert(Node previous_node, String data) {
        if(previous_node == null){
            System.out.println("Previous node can't be null");
            return;
        }
        Node new_node = new Node(data);

        new_node.next = previous_node.next;

        previous_node.next = new_node;
    }

    private static void insert(String data) {
        // check is head null or not
        if(head == null){
            // As head is null, so list is empty then create head node with given data
            head = new Node(data);
            return;
        }
        // head is not null so creating a object for the new node
        Node new_node = new Node(data);

        // assign head to new node address feild
        new_node.next = head;

        // move new node to head
        head = new_node;
    }

    static void printData(){
        while (head != null){
            System.out.println(head.data);
            head = head.next;
        }
    }
}