madil26
madil26

Reputation: 95

Trying to figure out size of a linked list null pointer error

For the below code, I would like to know why the size of the linked list keeps giving me a null pointer exeption and why my pushEnd method to push a new node at the end doesnt work, it add an element after a few nodes and gets rid of rest.

class Node {
int data;
Node next;
Node(int data){
    this.data = data;
}

}

public class LinkedList {
Node head;  

/* Inserts a new Node at front of the list. */
public Node push(int data) 
{ 
    Node newNode = new Node(data); 
    newNode.next = head; 
    return head = newNode; 
}

public Node pushEnd(int data) {
    Node newNode = new Node(data);
    if (head == null) {
        head = newNode;
    }

    newNode.next = null;

    while(head != null) {
        head = head.next;
        head.next = newNode;
        return newNode;
        }
    return head;
}

public int getSize() {
    int size = 0;
    while(this.head != null) {
        size++;
        head = head.next;
    }
    return size;
}


public void printList() {
    while (this.head !=null) {
        System.out.print(head.data + "-->");
        head = head.next;
    }       
    System.out.println(head);
}

}

public class Tester {


public static void main(String[] args) {

    LinkedList ll = new LinkedList();

    ll.push(35);
    ll.push(100);
    ll.push(14);
    ll.push(44);
    ll.push(10);
    ll.push(8);


    System.out.println("Created Linked list is:"); 
    ll.printList(); 



    System.out.println(ll.getSize());

}

}

I want to figure out the size of the linked list and be able to add nodes at the end.

Upvotes: 2

Views: 400

Answers (2)

mukesh210
mukesh210

Reputation: 2892

You are changing head reference, due to which you are getting incorrect output. You should make temp refer to head, do your operation using temp which will not affect head. It should be like below:

public class LinkedList {
    Node head;

    /* Inserts a new Node at front of the list. */
    public void push(int data) {
        Node newNode = new Node(data);
        newNode.next = head;
        head = newNode;
    }

    public void pushEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }

        newNode.next = null;
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = newNode;
    }

    public int getSize() {
        int size = 0;
        Node temp = head;
        while (temp != null) {
            size++;
            temp = temp.next;
        }
        return size;
    }


    public void printList() {
        Node temp = this.head;
        while (temp != null) {
            System.out.print(temp.data + "-->");
            temp = temp.next;
        }
        System.out.println(temp);
    }
}

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
    }
}

Upvotes: 2

nareddyt
nareddyt

Reputation: 1085

Your while loops modify the head variable directly. This causes your other code to fail because now head is pointing to the last node in the list.

Create a new local variable for use in the while loops (instead of modifying head directly). That should fix it!

Upvotes: 2

Related Questions