Linked List Components in Java10 May 2025 | 4 min read Given the head of a singly linked list and an integer array G representing a subset of node values. The task is to determine the number of connected components in the linked list that contain only values from G. Example 1 ![]() Input: Linked List: 0 -> 1 -> 2 -> 3 Subset G = {0, 1, 3} Output: 2 Explanation: The components {0, 1} and {3} are separate, as 2 is not in G, breaking the continuity. Example 2 ![]() Input: Linked List: 0 -> 1 -> 2 -> 3 -> 4 Subset G = {0, 3, 1, 4} Output: 2 Explanation: The components {0, 1} and {3, 4} are separate because 2 is not in G, breaking the continuity. Example 3 Input: Linked List: 1 -> 2 -> 3 -> 4 -> 5 Subset G = {2, 3, 4} Output: 1 Explanation: Nodes {2, 3, 4} are consecutive in the list, forming one connected component. Approach 1: Using HashSet ApproachThe HashSet approach to check whether a node's value belongs to the given subset G. AlgorithmStep 1: Store all values of G in a HashSet. Step 2: Traverse the linked list and check if the current node's value exists in the HashSet. Step 3: If the current node is in G and its next node is not in G (or is null), increment the count since it marks the end of a connected component. Step 4: Move to the next node and continue the process. Step 5: Return the total number of connected components. Output: 2 Time Complexity: The time complexity of a program is O(n). This is because we traverse the linked list once. Space Complexity: The space complexity of a program is O(k). This is because the HashSet stores up to k elements from G. Approach 2: Sorting and Binary Search ApproachThe binary search on a sorted array G to efficiently determine if a node's value is part of the subset. We traverse the linked list and count the number of connected components by checking contiguous nodes in G. AlgorithmStep 1: Sort the array G to enable fast lookups using binary search. Step 2: Initialize a count variable to keep track of the number of components. Step 3: Traverse the linked list: Step 3.1: Use binary search (Arrays.binarySearch()) to check if the current node is in G. Step 3.2: If a new component starts (node is in G but the previous was not), increment the count. Step 3.3: If the node is not in G, mark the end of a component. Step 4: Return the final count of connected components. Output: 2 Time Complexity: The time complexity of a program is O(n log k). This is because sorting takes O(k log k), traverses the linked list takes O(n) and binary search takes O(log k). Space Complexity: The space complexity of a program is O(1). This is because no extra space is used apart from input storage. Next TopicDijkstra Algorithm Java |
? Terminating an application in Java might also look like a straightforward challenge, however there are various techniques to gracefully terminate a given program or forcefully terminate it in case of surprising issues. In this section, we will discuss various ways to terminate a Java program and...
4 min read
Java is an object-oriented programming language that allows developers to create complex software systems. One of the key features of Java is inheritance, which allows classes to inherit properties and methods from other classes. In Java, a class can only extend one parent class at a...
4 min read
A concrete subclass of the Calendar class is referred to as GregorianCalendar. The GregorianCalendar class has an implementation of all of its inherited members. The Calendar class implements the mostly used Gregorian calendar. In order to use the Gregorian calendar in Java, we import the Java.util.GregorianCalendar...
16 min read
Finding the intersection of arrays with distinct elements in Java involves identifying common elements shared by two or more arrays. Since the elements are unique within each array, the task simplifies to efficiently comparing sets. This process is useful in various applications like data filtering, set...
8 min read
Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs. To learn the pattern program, we must have a...
13 min read
The LocalDate class in Java offers a mechanism to interact with dates without the time or time zone component as part of the Java 8 Date and Time API. This immutable class represents a date (year, month, and day) but not its time. It is frequently necessary...
4 min read
In Java, finding the number of digits in N factorial raised to the power of N is a fascinating puzzle. As N increases, the resulting number can become large, requiring careful handling. The task involves counting how many digits are in the final result and calls...
5 min read
A small frog is on a mission to cross a river. Initially located at position 0 on one bank, the frog wants to reach the opposite bank at position X+1. The river surface receives leaves that fall from a tree at various positions over time. More...
4 min read
The Thread class offers constructors and functions for creating and controlling threads. It serves as a subclass of Objects and also implements the Runnable interface. Deprecated methods are no longer considered significant and should not be used because they may be eliminated from the class in future...
6 min read
Java supports single-thread as well as multi-thread operations. A single-thread program has a single entry point (the main() method) and an exit point. A multi-thread program has an initial entry point (the main() method), followed by many entry and exit points that are run concurrently with...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India