1

So, im trying to learn the implementation of linked list in java. I created a method to insert in the start but when i run it through main, i don't see any output returned.

class ListNode{
  int data;
  ListNode next;

  ListNode(int data)
  {
    this.data = data;
    this.next = null;
  }
}

class Operations{
  static ListNode head = null;

  void insertStart(int num, ListNode head)
  {
    ListNode temp = new ListNode(num);
    if (head == null)
    {
      head = temp;
    }
    else{
      temp.next = head;
      head = temp;
    }
  }

  void display(ListNode head){
    ListNode temp = head;
    while (temp != null)
    {
      System.out.println(temp.data+"->");
      temp = temp.next;
    }
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5,head);
    l1.insertStart(4, head);
    l1.insertStart(3, head);
    l1.insertStart(2, head);
    l1.insertStart(1, head);
    l1.display(head);
  }
}

Can someone please help me, i can't seem to find the error. Everything seems perfect.

  • 2
    Thats because you are making changes to the parameter `ListNode head`. not the class variables `static ListNode head = null;`. You can either remove the ListNode parameter from the method, or use `this` to refer to head inside the method to resolve the issue. This also has to do with Java [pass by value or pass by reference](https://stackoverflow.com/q/40480/16034206), so you should read up on that to find out why `temp.next = head; head = temp;` did not work the way you expect. – experiment unit 1998X Apr 04 '23 at 06:08

2 Answers2

1

The problem is that you pass head as an argument to your insert method, and so it is a local variable. Any assignment to a local variable will not be seen by the caller.

But, taking a step back, it should not be necessary to pass head as argument to a method of your linked list instance. That instance should know "itself" what its head is. And that reveals another problem: head should not be a static member of your class, but an instance variable: every instance of Operations should have its own head.

Not a problem, but the implementation of insert doesn't really need the if...else construct. In either case you can assign temp.next=head, and in either case you want to set head=temp.

So correct your code as follows:

class ListNode{
  int data;
  ListNode next;

  ListNode(int data)
  {
    this.data = data;
    this.next = null;
  }
}

class Operations{
  ListNode head = null; // Should not be static! Every instance has its own head

  void insertStart(int num) // head should not be an argument. Use the instance variable.
  {
    ListNode temp = new ListNode(num);
    // No distinction needed between empty list or non-empty list:
    temp.next = head;
    head = temp;
  }

  void display(){ // head should not be an argument. Use the instance variable.
    ListNode temp = head;
    while (temp != null)
    {
      System.out.print(temp.data+"->"); // Output on same line
      temp = temp.next;
    }
    System.out.println("null"); // Terminate the line
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5); // Don't pass the head as argument.
    l1.insertStart(4);
    l1.insertStart(3);
    l1.insertStart(2);
    l1.insertStart(1);
    l1.display();
  }
}
trincot
  • 317,000
  • 35
  • 244
  • 286
-1

Here is what you need to do:

    class ListNode{
  int data;
  ListNode next;

  ListNode(int data)
  {
    this.data = data;
    this.next = null;
  }
}

class Operations{
  static ListNode head = null;

  void insertStart(int num)
  {
    ListNode temp = new ListNode(num);
    if (head == null)
    {
      head = temp;
    }
    else{
      temp.next = head;
      head = temp;
    }
  }

  void display(ListNode head){
    ListNode temp = head;
    while (temp != null)
    {
      System.out.println(temp.data+"->");
      temp = temp.next;
    }
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5);
    l1.insertStart(4);
    l1.insertStart(3);
    l1.insertStart(2);
    l1.insertStart(1);
    l1.display(head);
  }
}