Passing Node in the LinkedList doesn’t print anything

I pass head as a parameter in my add() method. I’m calling the add() method in public static void main(String args[]). It doesn’t print anything. But, if I don’t pass head as a parameter, then it prints the linked list. Why is this happening?

Code that is not printing anything:

 public class linkedListI {
        static Node head;
    
        static class Node {
            int data;
            Node next;
    
            Node(int data) {
                this.data = data;
            }
        }
    
        static void add(Node head, int data) {
            Node new_node = new Node(data);
    
            if (head == null) {
    
                head = new_node;
                head.next = null;
            }
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new_node;
            new_node.next = null;
    
        }
    
        static void printLinkedList(Node head) {
            Node curr = head;
            while (curr != null) {
                System.out.print(curr.data + " ");
                curr = curr.next;
            }
        }
    
        public static void main(String args[]) {
            // linkedListI l = new linkedListI();
            add(head, 3);
            add(head, 4);
            add(head, 6);
            add(head, 7);
            printLinkedList(head);
        }
    }

Code that gives the output:

This code prints the output but I am not passing head as a parameter in the add() method.

public class linkedListI {
    static Node head;

    static class Node {
        int data;
        Node next;

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

    static void add(int data) {
        Node new_node = new Node(data);

        if (head == null) {

            head = new_node;
            head.next = null;
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new_node;
        new_node.next = null;

    }

    static void printLinkedList(Node head) {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
    }

    public static void main(String args[]) {
        // linkedListI l = new linkedListI();
        add(3);
        add(4);
        add(6);
        add(7);
        printLinkedList(head);
    }
}

Answer

The reason is that in your first version, you have:

static void add(Node head, int data)

This means that any reference to head in this function will be the local variable, not the class member that is defined at the very top of your script.

As the parameter is a local variable, when you assign a new value to it, no-one will know about it, only the function’s execution context. The calling code will see no change in head, because arguments are passed by value. So head will remain null after every call to add.

If you want it to work that way — passing a head as argument — then change the signature of your add function, so that it returns the final value of head back to the caller, who must then reassign it back to its own head variable:

    static void add(Node head, int data) {
        Node new_node = new Node(data);

        new_node.next = null;
        if (head == null) {
            return new_node; // this is the new head
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new_node;
        return head; // head did not change
    }

// ....

    public static void main(String args[]) {
        head = add(head, 3);
        head = add(head, 4);
        head = add(head, 6);
        head = add(head, 7);
        printLinkedList(head);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *