Delete kinda works except for 1st and 2nd last element

This commit is contained in:
Matti 2024-05-06 18:05:13 +02:00
parent be8b24140e
commit ac503234b2
2 changed files with 18 additions and 14 deletions

View File

@ -10,16 +10,14 @@ public class Anwendung {
public static void main(String[] args){
LinkedList myList = new LinkedList();
myList.add(5);
myList.add("a");
myList.add("b");
myList.add("c");
myList.add("d");
myList.add("e");
myList.add(3.1415);
myList.add(1);
myList.add(1);
myList.add(1);
myList.add(2);
myList.add(3);
myList.add("f");
myList.delete(1);
myList.delete("e");
myList.printALL();
}
}

View File

@ -67,13 +67,19 @@ public class LinkedList<T>{
if (anchor.data == value){
anchor = anchor.next;
}
var next = anchor.next;
while (next != null){
if (next.next.data == value){
next.next = next.next.next;
return;
var cell = anchor.next;
while (cell != null){
if (cell.next != null) {
if (cell.next.data == value) {
if (cell.next.next != null) {
cell.next = cell.next.next;
return;
}
}
}
next = next.next;
cell = cell.next;
}
}
}