File tree 1 file changed +14
-3
lines changed
src/main/java/com/thealgorithms/datastructures/lists
1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,9 @@ else if (value > tail.value) {
47
47
}
48
48
newNode .next = temp .next ;
49
49
temp .next = newNode ;
50
+ if (newNode .next ==null ){
51
+ this .tail =newNode ;
52
+ }
50
53
}
51
54
}
52
55
@@ -63,25 +66,33 @@ public void display() {
63
66
* @param value the value to be deleted
64
67
* @return true if the element is found and deleted, false otherwise
65
68
*/
66
- public boolean delete (int value ) {
69
+ public boolean delete (int value ){
67
70
if (this .head == null ) {
68
71
return false ;
69
72
}
70
73
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
+ }
72
80
return true ;
73
81
}
74
82
else {
75
83
Node temp = this .head ;
76
84
while (temp .next != null ) {
77
85
if (temp .next .value == value ) {
86
+ if (temp .next == this .tail ) {
87
+ this .tail = temp ;
88
+ }
78
89
temp .next = temp .next .next ;
79
90
return true ;
80
91
}
81
92
temp = temp .next ;
82
93
}
83
94
return false ;
84
-
95
+
85
96
}
86
97
}
87
98
You can’t perform that action at this time.
0 commit comments