How to get a Sublist of LinkedList in Java

Introduction

One of the most crucial data structures to learn while preparing for interviews is the linked list. In a coding interview, having a thorough understanding of Linked Lists might be a major benefit.

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)

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


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

        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: O(end), where the end is the endIndex that is passed as a parameter. We have to traverse till the end index.

Space Complexity: O(k), where k is the number of elements in the sublist.

Approach and Algorithm (sublist from Linked List of LinkedLists)

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

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

        LinkedList > list
            = new LinkedList<>();

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

        LinkedList 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: O(end), where the end is the endIndex that is passed as a parameter. We have to traverse till the end index.

Space Complexity: O(k), where k is the number of elements in the sublist of LinkedLists.

So, in this article, we have tried to explain the most efficient way to get the Sublist of LinkedList in Java. Java Collection Framework is very important when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Previous post Bubble Sort for Linked List by Swapping Nodes
Next post LinkedList push() Method in Java

Leave a Reply

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