DeleteNode

Delete node from the linkedlist based on the given data (First occurrence data node).

  1. Create a method which receives data element as a argument.
  2. CheckĀ  is head node itself contains this data. if it is then change the head to next node of given data.
  3. if head doesn’t contain data then create a temp Node variable then assign head value to it and also create previous Node variable.
  4. traverse the loop till data contained node found and same time keep track of previous node.
  5. assign previous node next value to data node next value by skipping data node.
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;
}

Deleting node from the linkedlist with a given position.

  1. Check is list empty. if empty then print list is empty.
  2. Check given position is zero then assign head node to next node.
  3. If above 2 conditions not satisfied then we need to find a previous node of given position first for the first create a temp Node and assign it to head.
  4. traverse the loop till position-1 for find previous node.
  5. check previous node is null or previous node next is null, if any of one is null then position is exceeded than the list.
  6. If above condition fails then Assign next of next to temp next which points to position+1.
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;
}

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();
        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(4);

        printData();
    }

    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;
        }
    }
}

Output:

Vamsi
Santhosh
Muni
Basha