Skip to content
Advertisement

Custom LinkedList constructor with array as a parameter

I’ve written this program to implement a LinkedList. There are 2 classes: Node and IntLinkedList and the relationship between them is aggregation. In the IntLinkedList class, the constructor’s parameter is an array and I’ve tried to use a for-each loop to add the data of the array to the linked list. Here is my code:

Node.java

JavaScript

IntLinkedList.java

JavaScript

I’ve tried for many times but it still didn’t work.

Advertisement

Answer

In your code tmp is always null, so that when you call tmp.getNext() you will get NPE forever.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement