Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How to get a Sublist of LinkedList in Java

Last Updated on November 28, 2022 by Prepbytes

This blog will discuss various approaches To Get SubList from Linked List in Java. We have to figure out how to get a sublist of linked list in Java.
This question has its own importance as it is enhancing your knowledge about data structures like linked lists. Let’s discuss in detail how do you create a sublist from a linked list in Java.

LinkedList is a part of the Collection Framework, which is present in the java.util.package. This class implements the LinkedList data structure, which is a linear data structure in which the items are not stored in contiguous order and each item is a distinct object having a data and address portions.

Here in this problem, we need to find the items of a sublist of the given range, given a list of entries in a LinkedList.

Approach and Algorithm (Simple Sublist of LinkedList) To Get SubList from Linked List in Java

Syntax: public List subList (int startIndex, int endIndex)

Parameters: This method takes in the following parameters:

  • startIndex – The starting index of the sublist (inclusive).
  • endIndex – The ending index of the sublist (exclusive).

Return Value – This function returns a view of this list’s given range.

This function returns the sublist from the given starting index to the ending index.

Note: It does not include the element at the endIndex. We will just call the subList(int startIndex, int endIndex) function.

Input: [Coding, is, Fun], startIndex = 0 , endIndex = 2

Output: [Coding, is]

Explanation: As we can see, the elements from the startIndex(0) till the endIndex(2) (exclusive) were returned.

Code Implementation To Get SubList from Linked List in Java

import java.util.LinkedList;
import java.util.List;
  
public class PrepBytes {
    public static void main(String[] args)
    {
        LinkedList<string> list = new LinkedList<string>();
        list.add("Coding");
        list.add("is");
        list.add("Fun");
        System.out.println(
            "The elements of the  LinkedList are: " + list);
  
        int start = 0, end = 2;
  
        List sublist = list.subList(start, end);
        System.out.println("The required SubList is: "
                           + sublist);
    }
}
Output
The elements of the  LinkedList are: [Coding, is, Fun]
The required SubList is: [Coding, is]

Time Complexity To Get SubList from Linked List in Java: O(end), where the end is the endIndex that is passed as a parameter. We have to traverse till the end index.

Space Complexity To Get SubList from Linked List in Java: O(k), where k is the number of elements in the sublist.

Approach and Algorithm (sublist from Linked List of LinkedLists) To Get SubList from Linked List in Java

We can also get a sublist from a Linked List of LinkedLists using the exact same approach that is explained above. The syntax, parameters, and return type will be the same.

This function returns the sublist from the given starting index to the ending index.

Note: It does not include the element at the endIndex. We will just call the subList(int startIndex, int endIndex) function.

Input: [ [1,2], [3,4], [5,6] ], startIndex = 0 , endIndex = 2

Output: [ [1,2] , [3,4] ]

Explanation: As we can see, the sublist from the linked list of LinkedLists from the startIndex(0) till the endIndex(2) (exclusive) are returned.

Code Implementation To Get SubList from Linked List in Java

import java.util.LinkedList;
import java.util.List;
  
public class PrepBytes {
    public static void main(String[] args)
    {

        LinkedList<linkedlist<integer> > list
            = new LinkedList<>();

        LinkedList<integer> list1 = new LinkedList<>();
        list1.add(1);
        list1.add(2);
  
        LinkedList<integer> list2 = new LinkedList<>();
        list2.add(3);
        list2.add(4);

        LinkedList<integer> list3 = new LinkedList<>();
        list3.add(5);
        list3.add(6);

        list.add(list1);
        list.add(list2);
        list.add(list3);

        System.out.println(
            "The elements of the  LinkedList are: " + list);
  

        int start = 0, end = 2;
  
        List sublist = list.subList(start, end);
  
        System.out.println("The required SubList is: "
                           + sublist);
    }
}
Output
The elements of the  LinkedList are: [ [1, 2], [3, 4], [5, 6] ]
The required SubList is: [ [1, 2], [3, 4] ]

Time Complexity To Get SubList from Linked List in Java: O(end), where the end is the endIndex that is passed as a parameter. We have to traverse till the end index.

Space Complexity To Get SubList from Linked List in Java: O(k), where k is the number of elements in the sublist of LinkedLists.

This blog has tried to explain the most efficient way to get the Sublist of Linked List in Java. Java Collection Framework is very important when it comes to coding interviews. Solving questions from a linked list will make your data structures stronger and also enhance your logical abilities. For practicing more questions of linked list, you can follow this link Linked List.

FAQ

1. Why do we use sublists?
Sublist search is used to detect the presence of one list in another list. Suppose we have a single-node list (let’s say the first list), and we want to ensure that the list is present in another list (let’s say the second list), then we can perform the sublist search to find it.

2. Is subList inclusive?
subList() Method Parameters. fromIndex – start index in existing arraylist. It is inclusive.

3. What is called a nested list?
A nested list is a list that appears as an element in another list.

Leave a Reply

Your email address will not be published. Required fields are marked *