I would like a quick review on my method to delete a node from a singly linked list. I'm fairly certain that this is not near optimally done, and would love to get feedback.
It uses a SinglyLinkedNode
element as the node:
public class SinglyLinkedNode {
private SinglyLinkedNode next = null;
int data;
public SinglyLinkedNode(int data){
this.data=data;
}
public int getData(){
return this.data;
}
public void setNext(SinglyLinkedNode next){
this.next = next;
}
public SinglyLinkedNode getNext(){
return this.next;
}
}
The element definitions and the delete function are as so:
import data_structures.LinkedLists.SinglyLinkedNode;
public class SinglyLinkedList{
private SinglyLinkedNode head = null;
private SinglyLinkedNode tail = null;
private int length = 0;
public SinglyLinkedList(int data){
this.head = new SinglyLinkedNode(data);
this.tail = this.head;
this.length = 1;
}
public void delete(int data){
SinglyLinkedNode n = this.head;
if (n==null){
return;
}
if (n.getData()==data){//If the head is the data we want
if (n.getNext()==null){//If it's the only node, null the head and tail
this.tail = null;
this.head = null;
this.length = 0;
}
else {//Or move the head to the next node
this.head = n.getNext();
this.length--;
}
return;
}
while (n.getNext()!=null && n.getNext().getData()!=data){//Get the data node or the last node at n.next
n=n.getNext();
}
if (n.getNext() == null){//If n is the last element in the list
if (n.getData()!=data){//If data wasn't in array then we're done
return;
}
//If we're deleting the only remaining element
if (this.length <=1){
this.tail = null;
this.head = null;
this.length=0;
}
else {//If we're just moving the tail
n.setNext(null);
this.tail = n;
this.length--;
}
}
else {//If n is not the last element
if (n.getNext().getNext()==null){//If n.next is the last element
n.setNext(null);
this.tail = n;
this.length--;
}
else {//If n.next is not the last element
n.setNext(n.getNext().getNext());
this.length--;
}
}
}
}
As you can see, this is quite a lot of lines of code - 46 to be exact. In particular, the section below the while loop seems redundant - but I can't figure out how to replace any of it.