• Register
Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

linked list delete node in java

0 votes
30 views
linked list delete node in java
asked Sep 16, 2017 in Data Structure And Algorithm by Upendra Diwakar

2 Answers

0 votes
If you want to delete node from linked list at given position then visit the following URL:-
http://www.developerhelpway.com/forum/4050/delete-a-node-from-linked-list-at-a-given-position

Now, you see the following code to delete node from a linked list:-

package com.ds.linkedlist;

public class Node<T> {

    private T data;
    private Node<T> next;
   
    public Node(){
        setData(null);
        setNext(null);
    }
   
    public Node(T data){
        setData(data);
        setNext(null);
    }
   
    public T getData(){
        return data;
    }
   
    public void setData(T data){
        this.data = data;
    }
   
    public Node<T> getNext(){
        return next;
    }
   
    public void setNext(Node<T> next){
        this.next = next;
    }
   
}

package com.ds.linkedlist;

import java.util.HashMap;

public class SingleLinkedList<T> {
   
    Node<T> head = null;
    Node<T> reverseHead = null;
   
    public void addFirst(T data){
        if(data == null){
            return;
        }
        if(head == null){
            head = new Node<T>(data);
        }else{
            Node<T> newNode = new Node<T>(data);
            newNode.setNext(head);
            head = newNode;
        }
    }

      public void delete(T data){
        if(data == null){
            return;
        }
        Node<T> tempHead = head;
        Node<T> oldNode = null;
        while(tempHead != null){
            if(tempHead.getData() == data){
                if(oldNode == null){
                    tempHead = tempHead.getNext();
                    head = tempHead;
                }else{
                    oldNode.setNext(tempHead.getNext());
                }
                break;
            }
            oldNode = tempHead;
            tempHead = tempHead.getNext();
        }
    }

    }
}

package com.ds.linkedlist;

public class LinkedListTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SingleLinkedList<Integer> singleLinkedList = new SingleLinkedList<Integer>();
        singleLinkedList.addFirst(10);
//        singleLinkedList.addFirst(10);
        singleLinkedList.addFirst(20);
        singleLinkedList.addFirst(30);
        singleLinkedList.addFirst(80);
        singleLinkedList.addFirst(40);
        System.out.println("Node List:-");
        singleLinkedList.getNodeData();
               singleLinkedList.delete(10);
        System.out.println("After deletion linked list:-");
        singleLinkedList.getNodeData();
    }
}

Output:-
Node List:-
40 80 30 20 10
After deletion linked list:-
40 80 30 20
answered Sep 18, 2017 by ranju_12 (1,740 points)
0 votes
To delete node form given linked list see the folloiwng example with running code:-

//If num is exists
public void delete(T num){
    Node<T> temp = head;
    Node<T> prev = null;
    if(temp.getData() == num){
        temp = temp.getNext();
        head = temp;
    }
    while(temp != null){
        if(temp.getData() == num){
            prev.setNext(temp.getNext());
            return;
        }
        prev = temp;
        temp = temp.getNext();
    }
}
/**
* Handle following things:-
* 1. Delete head node
* 2. Not found node which have to delete
* 3. Found node which have to delete
*
*/
public void doDelete(T num){
    Node<T> temp = head, prev = null;
    //delete head node
    if(temp != null && temp.getData() == num){
        head = temp.getNext();
        return;
    }
    //Otherwise find num in linkedlist
    while(temp != null && temp.getData() != num){
        prev = temp;
        temp = temp.getNext();
    }
    //data not found in given linkedlist
    if(temp == null){
        System.out.println(num+" is not found in given LinkedList");
        return;
    }
    //Found then delete node
    prev.setNext(temp.getNext());
}
answered Sep 18, 2017 by me_vinod12 (1,960 points)
...