Skip to content
Advertisement

How would I find the length of a linkedlist IN a linkedlist?

I’m trying to go through a linkedlist(which we can call the superlist) which has elements of linked lists(sublists) within it.

The method that adds the elements into both linked lists is:

JavaScript

Now I have to write methods to check if there are groups of 1, 2, 3, 4 elements in the sublists by traversing the superlist What I have so far (which is very wrong) is:

JavaScript

Advertisement

Answer

What you can do is create a method that passes a parameter for the group size in question and finds out if there are any sub lists of size group size.

JavaScript

Side Note: As pointed out by @Abhishek your super List is technically not a LinkedList of LinkedList's. It is a LinkedList of Objects. That’s why you have to do the explicit cast to LinkedList to access the “Sub LinkedList’s”.

If you want a true LinkedList of LinkedList's, then you should create the superList like this:

JavaScript

If you create it that way, then you will avoid having to do the explicit casts at compile time.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement