Skip to main content
Add note about NPE in remove method
Source Link
mjolka
  • 16.3k
  • 2
  • 30
  • 73

There's a bug in the get method. If we run this code:

MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(1);
System.out.printf("%d %d%n", list.get(0), list.get(1));

the output is 1 1. I think you want to change

if(this.isEmpty() || index < 0 || index > this.nodeCount){

to

if (this.isEmpty() || index < 0 || index >= this.nodeCount) {

The following code will cause remove to throw a NullPointerException:

MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(1);
list.add(2);
list.remove(1);

There's a bug in the get method. If we run this code:

MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(1);
System.out.printf("%d %d%n", list.get(0), list.get(1));

the output is 1 1. I think you want to change

if(this.isEmpty() || index < 0 || index > this.nodeCount){

to

if (this.isEmpty() || index < 0 || index >= this.nodeCount) {

There's a bug in the get method. If we run this code:

MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(1);
System.out.printf("%d %d%n", list.get(0), list.get(1));

the output is 1 1. I think you want to change

if(this.isEmpty() || index < 0 || index > this.nodeCount){

to

if (this.isEmpty() || index < 0 || index >= this.nodeCount) {

The following code will cause remove to throw a NullPointerException:

MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(1);
list.add(2);
list.remove(1);
Source Link
mjolka
  • 16.3k
  • 2
  • 30
  • 73

There's a bug in the get method. If we run this code:

MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(1);
System.out.printf("%d %d%n", list.get(0), list.get(1));

the output is 1 1. I think you want to change

if(this.isEmpty() || index < 0 || index > this.nodeCount){

to

if (this.isEmpty() || index < 0 || index >= this.nodeCount) {