Namrata Shukla
Namrata Shukla

Reputation: 157

inserting node in a linklist using java

I m beginner at java. I am trying to implement simple linklist structure using java.

I have written following code that inserts node at the end of the linklist.

 public static  Node insert(Node head,int data) {
    if(head == null)
    {
      Node temp = new Node(data);
        head = temp;
        return head;
    }
    else
    {
         Node temp = new Node(data);
    Node current = head;
    while(current != null)
    {
        current = current.next;
    }
    current = temp;
    return head;
    }
}

The Node class is defined as follows

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

The class LinkListDemo has the insert(),display() and main() method as follows..

 class LinkListDemo
 {
 public static  Node insert(Node head,int data) {
    if(head == null)
    {
      Node temp = new Node(data);
        head = temp;
        return head;
    }
    else
    {
         Node temp = new Node(data);
    Node current = head;
    while(current != null)
    {
        current = current.next;
    }
    current = temp;
    return head;
    }
}
public static void display(Node head) {
    Node start = head;
    while(start != null) {
        System.out.print(start.data + " ");
        start = start.next;
    }
}

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    Node head = null;
    int N = sc.nextInt();

    while(N-- > 0) {
        int ele = sc.nextInt();
        head = insert(head,ele);
    }
    display(head);
    sc.close();
}
}

INPUT: 4 2 3 4 1

I gave input as 4(number of nodes to be inserted) 2 3 4 1(corresponding node values)

I expected output to be 2 3 4 1 but the output is only 2.

Please help me to correct my mistake. Thanks in advance.

Upvotes: 0

Views: 104

Answers (4)

tech.mohit.garg
tech.mohit.garg

Reputation: 680

Just Change little bit code

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

and return head

Upvotes: 0

tech.mohit.garg
tech.mohit.garg

Reputation: 680

I am inserting node using following code

public void addFirst(int e) {
        if (head == null) {
            head = new LinkListNode(e);
            tail = head;
            size = 1;
        } else {
            LinkListNode nextNode = new LinkListNode(e);
            nextNode.setNext(head);
            head = nextNode;
            size++;
        }
    }

Working fine...

Upvotes: 0

samuelli97
samuelli97

Reputation: 71

In your code for insert(), you should have

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

In your code, your current variable will always end up being null, resulting in your node not actually being inserted. This fix makes your current variable the last node in the list, so that you can set the last node's pointer to your new node.

Upvotes: 0

Thiyagu
Thiyagu

Reputation: 17880

The problem is in the else part of your insert method. You are looping till current becomes null and then assign the new node temp to it. Assigning the reference to the new node(temp) will not append (or link) to the end of the list.

The correct way is to go to the last node and then link the new node i.e, make the last node's next point to the new node.

It should be like

while(current.next != null) {
    current = current.next;
}
current.next = temp;

Upvotes: 1

Related Questions