Quantcast
Channel: How to detect a loop in a linked list? - Stack Overflow
Browsing all 30 articles
Browse latest View live

Answer by Sonu Mishra for How to detect a loop in a linked list?

// linked list find loop function int findLoop(struct Node* head) { struct Node* slow = head, *fast = head; while(slow && fast && fast->next) { slow = slow->next; fast =...

View Article



Image may be NSFW.
Clik here to view.

Answer by Neil for How to detect a loop in a linked list?

In this context, there are loads to textual materials everywhere. I just wanted to post a diagrammatic representation that really helped me to grasp the concept. When fast and slow meet at point p,...

View Article

Answer by vishwaraj for How to detect a loop in a linked list?

Here is the solution for detecting the cycle. public boolean hasCycle(ListNode head) { ListNode slow =head; ListNode fast =head; while(fast!=null && fast.next!=null){ slow = slow.next; // slow...

View Article

Answer by xnorcode for How to detect a loop in a linked list?

You may use Floyd's tortoise algorithm as suggested in above answers as well. This algorithm can check if a singly linked list has a closed cycle. This can be achieved by iterating a list with two...

View Article

Answer by Irshad ck for How to detect a loop in a linked list?

Here is my solution in java boolean detectLoop(Node head){ Node fastRunner = head; Node slowRunner = head; while(fastRunner != null && slowRunner !=null && fastRunner.next != null){...

View Article


Answer by Aditya Parmar for How to detect a loop in a linked list?

boolean hasCycle(Node head) { boolean dec = false; Node first = head; Node sec = head; while(first != null && sec != null) { first = first.next; sec = sec.next.next; if(first == sec ) { dec =...

View Article

Answer by Sarthak Mehra for How to detect a loop in a linked list?

This code is optimized and will produce result faster than with the one chosen as the best answer.This code saves from going into a very long process of chasing the forward and backward node pointer...

View Article

Answer by Richa for How to detect a loop in a linked list?

// To detect whether a circular loop exists in a linked list public boolean findCircularLoop() { Node slower, faster; slower = head; faster = head.next; // start faster one node ahead while (true) {...

View Article


Answer by Habib Karbasian for How to detect a loop in a linked list?

Here is my runnable code. What I have done is to reveres the linked list by using three temporary nodes (space complexity O(1)) that keep track of the links. The interesting fact about doing it is to...

View Article


Answer by rai.skumar for How to detect a loop in a linked list?

This approach has space overhead, but a simpler implementation: Loop can be identified by storing nodes in a Map. And before putting the node; check if node already exists. If node already exists in...

View Article

Answer by Adit Ya for How to detect a loop in a linked list?

I might be terribly late and new to handle this thread. But still.. Why cant the address of the node and the "next" node pointed be stored in a table If we could tabulate this way node present:...

View Article

Answer by Khaled.K for How to detect a loop in a linked list?

Algorithm public static boolean hasCycle (LinkedList<Node> list) { HashSet<Node> visited = new HashSet<Node>(); for (Node n : list) { visited.add(n); if (visited.contains(n.next)) {...

View Article

Answer by Ashok Bijoy Debnath for How to detect a loop in a linked list?

Better than Floyd's algorithm Richard Brent described an alternative cycle detection algorithm, which is pretty much like the hare and the tortoise [Floyd's cycle] except that, the slow node here...

View Article


Answer by edst for How to detect a loop in a linked list?

public boolean isCircular() { if (head == null) return false; Node temp1 = head; Node temp2 = head; try { while (temp2.next != null) { temp2 = temp2.next.next.next; temp1 = temp1.next; if (temp1 ==...

View Article

Answer by Eduardo for How to detect a loop in a linked list?

You could even do it in constant O(1) time (although it would not be very fast or efficient): There is a limited amount of nodes your computer's memory can hold, say N records. If you traverse more...

View Article


Answer by Abhinav for How to detect a loop in a linked list?

Detecting a loop in a linked list can be done in one of the simplest ways, which results in O(N) complexity using hashmap or O(NlogN) using a sort based approach. As you traverse the list starting from...

View Article

Answer by Dave L. for How to detect a loop in a linked list?

Here's a refinement of the Fast/Slow solution, which correctly handles odd length lists and improves clarity. boolean hasLoop(Node first) { Node slow = first; Node fast = first; while(fast != null...

View Article


Answer by Carl Mäsak for How to detect a loop in a linked list?

The user unicornaddict has a nice algorithm above, but unfortunately it contains a bug for non-loopy lists of odd length >= 3. The problem is that fast can get "stuck" just before the end of the...

View Article

Answer by smessing for How to detect a loop in a linked list?

If we're allowed to embed the class Node, I would solve the problem as I've implemented it below. hasLoop() runs in O(n) time, and takes only the space of counter. Does this seem like an appropriate...

View Article

Answer by smessing for How to detect a loop in a linked list?

public boolean hasLoop(Node start){ TreeSet<Node> set = new TreeSet<Node>(); Node lookingAt = start; while (lookingAt.peek() != null){ lookingAt = lookingAt.next; if...

View Article
Browsing all 30 articles
Browse latest View live




Latest Images