1

I'm trying this method but I can't figure out the problem. I think that there is some problem in insert() method because I have not used recursion but i can't exactly pinpoint it. Thanks in advance.

import java.io.*;
import java.util.*;

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

class Solution {
public static  Node insert(Node head,int data) {
        //Some problem in this method
        if(head==null)
            return new Node(data);
        else{
            Node nxt = head;
            while(nxt!=null)
                nxt = nxt.next;
            nxt = new Node(data);
            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();
    }
}
1
  • nxt is loose, it's not connected to anything! Commented Jun 24, 2017 at 6:04

3 Answers 3

2

Try with the below code.

class Nodes {
  int data;
  Nodes next;

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


/**
 * Insert in linkedList
 * 
 * @author asharda
 *
 */
public class LinkedListSum {


  public Nodes insert(Nodes node, int data) {
    Nodes temp;
    if (node == null) {
      node = new Nodes(data);
      return node;
    } else {
      temp = node;
      while (temp.next != null) {
        temp = temp.next;
      }
      temp.next = new Nodes(data);
      return node;
    }
  }// end of insert

  /**
   * Display LinkedList
   * 
   * @param root
   */
  public void display(Nodes root) {
    while (root != null) {
      System.out.println(root.data);
      root = root.next;
    }
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    LinkedListSum sum = new LinkedListSum();
    Nodes head = null;
    head = sum.insert(head, 3);
    head = sum.insert(head, 1);
    head = sum.insert(head, 5);
    sum.display(head);
  }

}
Sign up to request clarification or add additional context in comments.

Comments

0

You don't want to do

 nxt = new Node(data);

That won't actually alter the list - it'll just make nxt refer to a new Node instead of the node at the end of the list. You actually want to do

 nxt.next = new Node(data);

Comments

0

when inserting a new Node, next of previous Node must be set to this new Node - nxt = new Node(data) only sets the variable. Hint: end the loop when next of the node is null instead of the node being null (rename the loop variable from nxt to node or last) and set its next to the new Node.

In other words, find the last node and set its next to the new node.

Suggested improvement: instead of returning the head of the list, just save it as a field of the list class. Initially it is null and set to the first node that is inserted. This avoids you haven't to maintain it outside of the list...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.