File tree 2 files changed +25
-3
lines changed
main/java/com/thealgorithms/datastructures/lists
test/java/com/thealgorithms/datastructures/lists 2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,12 @@ public E get(int index) {
100
100
return current .value ;
101
101
}
102
102
103
+ /*
104
+ java.lang.NullPointerException: Cannot invoke
105
+ "com.thealgorithms.datastructures.lists.SkipList$Node.setPrevious(int, com.thealgorithms.datastructures.lists.SkipList$Node)"
106
+ because the return value of "com.thealgorithms.datastructures.lists.SkipList$Node.next(int)" is null
107
+ at com.thealgorithms.datastructures.lists.SkipListTest.remove(SkipListTest.java:50)
108
+ */
103
109
public void remove (E e ) {
104
110
Objects .requireNonNull (e );
105
111
Node <E > current = head ;
@@ -117,7 +123,9 @@ public void remove(E e) {
117
123
}
118
124
for (int i = 0 ; i <= layer ; i ++) {
119
125
current .previous (i ).setNext (i , current .next (i ));
120
- current .next (i ).setPrevious (i , current .previous (i ));
126
+ if (current .next (i ) != null ) {
127
+ current .next (i ).setPrevious (i , current .previous (i ));
128
+ }
121
129
}
122
130
size --;
123
131
}
Original file line number Diff line number Diff line change @@ -42,12 +42,26 @@ void contains() {
42
42
}
43
43
44
44
@ Test
45
- void remove () {
45
+ void removeFromHead () {
46
46
SkipList <String > skipList = createSkipList ();
47
+ String mostLeftElement = skipList .get (0 );
47
48
int initialSize = skipList .size ();
48
49
print (skipList );
49
50
50
- skipList .remove ("a" );
51
+ skipList .remove (mostLeftElement );
52
+
53
+ print (skipList );
54
+ assertEquals (initialSize - 1 , skipList .size ());
55
+ }
56
+
57
+ @ Test
58
+ void removeFromTail () {
59
+ SkipList <String > skipList = createSkipList ();
60
+ String mostRightValue = skipList .get (skipList .size () - 1 );
61
+ int initialSize = skipList .size ();
62
+ print (skipList );
63
+
64
+ skipList .remove (mostRightValue );
51
65
52
66
print (skipList );
53
67
assertEquals (initialSize - 1 , skipList .size ());
You can’t perform that action at this time.
0 commit comments