Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me how I can able to create a LinkedList class from a scratch using Java?

1 Answer

0 votes
by (26.7k points)

Basically, you can create two classes for this one which is LinkNode and another LinkedList. You can also follow the below code as a reference:

public class LinkNode { 

    String data;

    LinkNode next;

    public LinkNode(String item) { 

       data = item;

    }

}

public class LinkedList { 

    LinkNode head;

    public LinkedList(String item) { 

       head = new LinkNode(item);

    }

    public void add(String item) { 

       //pseudo code: while next isn't null, walk the list

       //once you reach the end, create a new LinkNode and add the item to it.  Then

       //set the last LinkNode's next to this new LinkNode

    }

}

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java Expert? Join Java Certification now!!

Related questions

0 votes
1 answer
0 votes
1 answer
asked Apr 4, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...