Remove Loop in Linked List Using Java5 May 2025 | 6 min read A loop in a linked list forms when a node points back to an earlier node, creating a cycle instead of ending the list. Detecting and removing this loop restores the list's linear structure, preventing infinite traversal and improving its reliability for further operations. Approach: Using HashingThis method uses a HashSet to store visited nodes while traversing the linked list. If a node is revisited (already in the HashSet), a loop is detected and removed by updating the previous node's next pointer to null. AlgorithmStep 1: Create an empty HashSet to store visited nodes and a variable previousNode to track the previous node. Step 2: Start from the head node and traverse through the linked list, checking each node. Step 3: For each node, check if it's already in the HashSet (indicating a loop). If a loop is detected, remove it by setting previousNode.next = null. Step 4: If no loop is detected, add the current node to the HashSet and move to the next node. Step 5: Continue until the end of the list is reached (i.e., headNode becomes null), ensuring the loop is removed if it exists. ImplementationFile Name: LinkedListLoopRemover.java Output: 5 12 7 3 8 Time Complexity: O(N) - Traverses each node once. Auxiliary Space: O(N) - Uses a HashSet to store visited nodes. Approach: Floyd's Cycle Detection AlgorithmFloyd's Cycle Detection Algorithm, also known as the Tortoise and Hare approach, uses two pointers moving at different speeds to detect a loop in a linked list. If a cycle exists, the two pointers will meet inside the loop, allowing for efficient detection and removal with constant space complexity. AlgorithmStep 1: Set two pointers, slowPointer and fastPointer, at the head of the linked list. Step 2: Move slowPointer one step and fastPointer two steps forward. If they meet, a loop is detected. Step 3: Reset slowPointer to the head. Move both slowPointer and fastPointer one step at a time until they meet at the start of the loop. Step 4: Once the start of the loop is found, set the last node's nextNode pointer to null to break the loop. Step 5: Print the linked list from the head to confirm the loop is removed. ImplementationFile Name: LinkedListLoopRemover.java Output: 100 200 300 400 500 Time Complexity: O(n) Auxiliary Space Complexity: O(1) Next TopicKeith Number in Java |
Prime numbers have always fascinated mathematicians due to their unique properties and applications in various fields. One such intriguing aspect of prime numbers is circular primes, which are primes that remain prime when their digits are rotated cyclically. In this article, we will delve into circular...
6 min read
ORM stands for Object Relation Mapping. It is a middleware application or tool that sits between the web application and database. It wraps the implementation specific details of storage drivers in an API. What is ORM? ORM is a technique for converting data between Java objects and relational...
3 min read
Java 15 or JDK 15 is the reference implementation of the Java SE Platform with the version 15. It is released as an important feature and base for the Java17. Java15 provides various new features, which are very exciting, incubator features and iew features for the JDK...
12 min read
In Java, public and private are keywords that are known as an access modifier or specifier. It restricts the scope or accessibility of a class, constructor, variables, methods, and data members. It depends on which it is applied. Java provides the four types of access...
6 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Flipkart etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
5 min read
James Gosling created Java, a high-level programming language, in 1995. A popular language for Android apps is Java. Java is used even in the creation of the Android operating system. Because of its clear, straightforward, and understandable syntax, it is very well-liked by developers. Over the...
3 min read
Java, being an object-oriented programming language, encourages the use of modular code to improve maintainability and reusability. One of the key features that facilitate code organization is the concept of packages. Packages in Java serve as containers for related classes, interfaces, and other resources, allowing developers...
5 min read
Padding a string in Java refers to the practice of appending specific characters, typically spaces or a chosen character, to the start, end, or both sides of a string. The technique is employed to reach a specified length or to align text uniformly. It's a technique...
5 min read
The java.nio.DoubleBuffer has a duplicate() method. The DoubleBuffer Class is used to create a new float buffer that shares the contents of the given buffer. The buffer's contents will be making up the new buffer. The new buffer will reflect changes made to this buffer's content...
3 min read
The java.util package contains the LongSummaryStatistics class. When dealing with a stream of long integers, it accepts a collection of Long objects and can be advantageous. It keeps track of how many values it has processed, how much they have added up to, and other...
4 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