elo melo
elo melo

Reputation: 1

Insert a node at a specific position in a linked list JAVA

public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
    if(llist == null) {
        llist =  new SinglyLinkedListNode(data);
        return llist;
    } else {
        for (int i = 0; i < position-1; i++) {
            llist = llist.next;
        }
        SinglyLinkedListNode temp = llist;
        llist.next = new SinglyLinkedListNode(data);
        llist = llist.next;
        llist.next = temp.next;         
        return llist;
    }
}

This is my code to place a custom index node in LinkedList. But hackerrank is not accepting my code. What's wrong with my algorithm?

Upvotes: 0

Views: 1273

Answers (5)

Govind Sharma
Govind Sharma

Reputation: 137

single linkedlist insert value dafault and by position

 public class Main {
    
    private Node head;
    
     public void insert(int data,int position){
        Node newNode=new Node(data);
        if(head==null){
            head=newNode;
            return;
        }
        Node current=head;
        int counter=0;
        boolean flag=true;
        while(current.next!=null){
            if(counter==position){
                current.data=data;
                flag=false;
            }
            current=current.next;
            counter++;
        }
        if(flag) {
            current.next=newNode;
        }
    }
    
    public void insert(int data){
        Node newNode=new Node(data);
        if(head==null){
            head=newNode;
            return;
        }
        Node current=head;
        while(current.next!=null){
            current=current.next;
        }
        current.next=newNode;
    }
    
    public void display(){
        Node current=head;
        while(current!=null){
            System.out.println(current.data);
            current=current.next;
            
        }
    }
    public static void main(String[] args) {
        System.out.println("Try programiz.pro");
        Main obj=new Main();
        obj.insert(10);
        obj.insert(20);
        obj.insert(30);
        obj.insert(40);
        obj.insert(70,4);
        obj.display();
    }
}

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

10 20 30 40 70

Upvotes: 0

Mabadai
Mabadai

Reputation: 355

Suppose given the correct Node class, you can try this approach (without index collision case):

private Node find(int index) {
    Node curr = head;
    for (int i = 0; i < index; i++)
        curr = curr.next;

    return curr;
}   // end find()

public Object get(int index) throws IndexOutOfBoundsException {
    if (index >= 0 && index < size) {
        Node curr = find(index);
        return curr.data;
    } else {
        throw new IndexOutOfBoundsException();
    }   // end if - else
}   // end get()

public void add(Object data, int index) throws IndexOutOfBoundsException {
    if (index >= 0 && index < size + 1) {
        if (index == 0)
            head = new Node(data);
        else {
            Node prev = find(index - 1);
            prev.next = new Node(data);
        } // end if - else
        size++;
    } else {
        throw new IndexOutOfBoundsException();
    }   // end if - else
}   // end add()

Upvotes: 0

Cui Tony
Cui Tony

Reputation: 1

The problem requires you to return a linked list. When we are asked to return a linked list, actually we return the first node of the linked list.

So, your problem is the returned value in your code script is not the first node of the linked list.

The simplest solution is that you keep the first node in another variable, and
return that variable after you have done the insert thing. for example:

SinglyLinkedListNode dumyNode = llist;
......
return dumyNode;

Upvotes: 0

VAIBHAV NIRMAL
VAIBHAV NIRMAL

Reputation: 384

Easiest solution No need of explaination : Solution :

static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {
        if (head == null) return null;
        SinglyLinkedListNode temp = new SinglyLinkedListNode(data);
        if (position == 0) {
            temp.next = head;
            return temp;
        }
        SinglyLinkedListNode p = head;
        for (int i = 0; i< position-1; i++) {
            p = p.next;
        }
        SinglyLinkedListNode next = p.next;
        p.next = temp;
        temp.next = next;
        return head;
    }

Upvotes: 0

trincot
trincot

Reputation: 350781

The problem is that your code always returns the newly created node, but you should always return the first node in the list, which either is what it was when you got it, or is the new node in case the position was zero.

What to look at:

I'll not provide the corrected code, but will give you two hints:

By moving llist ahead in the for loop, you lose the reference to that first node, so use a different variable for walking through the list.

Also, you should deal specifically with the case where position is 0, as this is the only case where the returned value is not the original llist value, but the new node's reference, much like you have in the if block.

Upvotes: 0

Related Questions