0

I'm struggling with inserting an new element at the end of Linked-list. The problem appears at the while-loop "NullPointException". Could anybody help?

 // Node head = null;

@Override
public void add( T newvalue ) {
    Node newNode = new Node (newvalue);
    newNode.next = head; 
    head = newNode;
}
Mido
  • 41
  • 4
  • 3
    Could you add your part of the code that's not working ? – Tapaka Mar 14 '18 at 16:19
  • 2
    I don't see any potential issue here. Post full code – Kaushal28 Mar 14 '18 at 16:20
  • should you be assigning the new node as head? I would think you would want to do something like `currentNode.next = newNode; newNode.next = head;` – badperson Mar 14 '18 at 16:26
  • 1
    Can you be more specific about your input and expected output? – Jimmy Mar 14 '18 at 17:39
  • I use one-dimensional-Linked array (not double). I took mistake with name "head", I had to name it "start". The list have three elements and I wanna add one more and get the first element (as a queue). class Linklist { private Node start; private class Node { T value; Node next; public Node (T v) { this.value = v;} } public void addelement( T value ) { Node newNode = new Node (value ); while (start.next != null) { start = start.next ; } newNode = start.next ; } } – Mido Mar 14 '18 at 23:42

1 Answers1

0

Your code is not correct for adding to the tail of a linked list. A linked list should add a new node at the END of the current or last node. Since you only show that head is a node and more than likely this is a single pointer node, let's assume that head is what will point to the end of the node (you can change the name to tail). Your code would need to look at the last node (head in this case), and add the new node to it, then update the reference to the new node.

// Node head = null;

    @Override
    public void add( T newvalue ) {
        Node newNode = new Node (newvalue); //define new node
        if(head !=null){//make sure what you are pointing to exists
            head.next = newNode; // set the next point to newNode
            head = newNode; //change the reference to your tail (called head here)
        } else{
            head = newNodel; //if doesn't exist, just point to new node
        }
    }

That's the jist of what you want. Honestly, you will want a head node and a tail node. Without the head node always pointing to the head, you will not be able to traverse the list. Head should point to the first node always and tail should point to the last node. At first, head and tail will point to the same null or 1st node. Then you move tail in the add function.

eDog
  • 173
  • 1
  • 5
  • I use one-dimensional-Linked array (not double). I took mistake with name "head", I had to name it "start". The list have three elements and I wanna add one more and get the first element (as a queue). class Linklist { private Node start; private class Node { T value; Node next; public Node (T v) { this.value = v;} } public void addelement( T value ) { Node newNode = new Node (value ); while (start.next != null) { start = start.next ; } newNode = start.next ; } } – Mido Mar 15 '18 at 01:31