0

I created a simple node class, and solution class with an insert method, a display method, along with Main. I am trying to insert 4 numbers into the linked-list and have it display the entire list. At most I am only able to have it display 2 of the numbers. The issue is most likely in my insert method. I've spent hours trying to figure out what the issue is. What could be wrong with my code?

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

public static void display(Node head)
    {
        Node start = head;
        while (start != null)
        {
            Console.Write(start.data + " ");
            start = start.next;
        }
    }

static void Main(String[] args)
    {

        Node head = null;
        int[] numbers = new int[]{2, 3, 4, 1};
        for (int i = 0; i < numbers.Length; i++)
        {
            int data = numbers[i];
            head = insert(head, data);
        }
        display(head);
        Console.ReadLine();
    }

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

}
4
  • 1
    Do you want to return head or inserted node - newNode? Commented Jun 29, 2022 at 21:00
  • I am trying to return the head. By doing so, my display method would return the entire list. Commented Jun 29, 2022 at 21:14
  • Could you, please, provide Display method, Node class with your failed test? If you want to return head, Insert method looks OK Commented Jun 29, 2022 at 21:22
  • Okay, I've just provided them. It should return 2,3,4,1 but it's just returning 4,1 Commented Jun 29, 2022 at 21:30

1 Answer 1

2

Yes, the problem is in the insert method:

   while (head.next != null)
   {
        // From now on you have the initial head got lost
        head = head.next;
   }

Quick amendment is to change while into for:

    public static Node insert(Node head, int data) {
      Node newNode = new Node(data);
      
      if (head == null) {
        head = newNode;
      }
      else {
        // we loop on last, keeping head intact 
        for (Node last = head; ; last = last.next)  
          if (last.next == null) {
            last.next = newNode;

            break;
          }
      }
      return head;
    }

we can simplify it further:

    public static Node insert(Node head, int data) {
      Node newNode = new Node(data);

      for (Node last = head; last != null; last = last.next)
        if (last.next == null) {
          last.next = newNode;

          return head;
        }

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

2 Comments

@UniqueHold: you can keep while loop, but in this case you should save the initial head in order to return it at the end.
Thanks! That info was just what I needed to make it work. Now I'm able to do it with either loops, I just need to better understand the concept behind it and I should be good to go.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.