1
votes

I have created a method to insert a node in LinkedList at Nth index. The code works fine for all the positions except 0th position. Please review my code below and let me know what's wrong. The below code has 2 insert methods:

  1. insert(int data): This used to add a new node.
  2. insertAtNthPositon(int position, int data): This helps to insert the element at Nth position.
public class LinkedList {
    Node head;

    static class Node {
        int data;
        Node next;
    }

    public void insert(int data) {
        Node new_node = new Node();
        new_node.data = data;
        new_node.next = null;

        if (head == null) {
            head = new_node;
        } else {
            Node n = head;
            while (n.next != null) {
                n = n.next;
            }
            n.next = new_node;
        }
    }

    public void insertAtNthPositon(int position, int data) {
        Node new_node = new Node();
        new_node.data = data;
        Node current_node = head;

        int i = 0;
        if (position == 0) {
            new_node.next = current_node.next;
            head = new_node;
            current_node = current_node.next;
        }
        while (i < position - 1) {
            current_node = current_node.next;
            i++;
        }
        new_node.next = current_node.next;
        current_node.next = new_node;
    }

    public static void main(String[] arg) {

        LinkedList ls = new LinkedList();
        ls.insert(6);
        ls.insert(7);
        ls.insert(9);
        ls.insertAtNthPositon(1, 100);
        ls.show();

    }

    private void show() {
        Node current = head;
        StringBuilder sb = new StringBuilder();
        do {
            sb.append(current.data);
            sb.append("->");
            current = current.next;
        } while (current != null);
        sb.append("null");
        System.out.println(sb.toString());
    }
}

Output:

6->100->7->9->null

1
There are some variables/parameters you haven't defined and the top of your code starts in the middle of a method. Is that the insert(..) or insertAtNthPosition(..) method? What is the difference between these two methods? Perhaps you don't need both and you only need one method (they seem to do the same thing)? Are you actually using java.util.LinkedList ? Last I checked it doesn't have insert methods. - dan
As mentioned above, some details are missing, but in here: new_node.next = current_node.next, I think new_node.next should point to current_node instead of current_node.next - Gem741
If head is initialized to null, then this line: new_node.next = current_node.next; will generate an NPE. You need to provide more in your code above to tell us what you're doing to determine why 0 is failing as an index for your homework assignment. Otherwise, you should be using java.util.ArrayList. - dan
Just made some edits to the code for more clarity. - 06111
Just as I suggested: you need to set the new_node.next to current_node and not current_node.next -> this is why you always start from index 1 and not 0. - Gem741

1 Answers

0
votes

Just as I suggested: you need to set [at: if (position == 0)] the new_node.next to current_node and not current_node.next -> this is why you always start from index 1 and not 0.

   public void insertAtNthPositon(int position, int data){
    Node new_node = new Node();
    new_node.data =data;
    Node current_node = head;

    int i = 0;
    if (position == 0){
        **new_node.next = current_node;** <instead of current_node.next>
        head = new_node;
        current_node = current_node.next;
    }
    while(i < position - 1){
        current_node = current_node.next;
        i++;
    }
    new_node.next = current_node.next;
    current_node.next = new_node;
}

BTW- ls.insertAtNthPositon(1, 100); insert 100 at index 1, you want to do ls.insertAtNthPositon(0, 100); in order for '100' to be the first.