Skip to content

Commit 58390c1

Browse files
committed
Updated tail
1 parent 2c33f89 commit 58390c1

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ else if (value > tail.value) {
4747
}
4848
newNode.next = temp.next;
4949
temp.next = newNode;
50+
if (newNode.next==null){
51+
this.tail=newNode;
52+
}
5053
}
5154
}
5255

@@ -63,25 +66,33 @@ public void display() {
6366
* @param value the value to be deleted
6467
* @return true if the element is found and deleted, false otherwise
6568
*/
66-
public boolean delete(int value) {
69+
public boolean delete(int value){
6770
if (this.head == null) {
6871
return false;
6972
}
7073
else if (this.head.value == value) {
71-
this.head = this.head.next;
74+
if (this.head.next == null) {
75+
this.head = null;
76+
this.tail = null;
77+
} else {
78+
this.head = this.head.next;
79+
}
7280
return true;
7381
}
7482
else{
7583
Node temp = this.head;
7684
while (temp.next != null) {
7785
if (temp.next.value == value) {
86+
if (temp.next == this.tail) {
87+
this.tail = temp;
88+
}
7889
temp.next = temp.next.next;
7990
return true;
8091
}
8192
temp = temp.next;
8293
}
8394
return false;
84-
95+
8596
}
8697
}
8798

0 commit comments

Comments
 (0)