Quantcast
Channel: How to detect a loop in a linked list? - Stack Overflow
Browsing latest articles
Browse All 30 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

Answer by meriton - on strike for How to detect a loop in a linked list?

An alternative solution to the Turtle and Rabbit, not quite as nice, as I temporarily change the list: The idea is to walk the list, and reverse it as you go. Then, when you first reach a node that has...

View Article


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

I cannot see any way of making this take a fixed amount of time or space, both will increase with the size of the list. I would make use of an IdentityHashMap (given that there is not yet an...

View Article


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

You can make use of Floyd's cycle-finding algorithm, also known as tortoise and hare algorithm. The idea is to have two references to the list and move them at different speeds. Move one forward by 1...

View Article

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

The following may not be the best method--it is O(n^2). However, it should serve to get the job done (eventually). count_of_elements_so_far = 0; for (each element in linked list) { search for current...

View Article

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

Tortoise and hare Take a look at Pollard's rho algorithm. It's not quite the same problem, but maybe you'll understand the logic from it, and apply it for linked lists. (if you're lazy, you can just...

View Article


Image may be NSFW.
Clik here to view.

How to detect a loop in a linked list?

Say you have a linked list structure in Java. It's made up of Nodes: class Node { Node next; // some user data } and each Node points to the next node, except for the last Node, which has null for...

View Article

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

func checkLoop(_ head: LinkedList) -> Bool { var curr = head var prev = head while curr.next != nil, curr.next!.next != nil { curr = (curr.next?.next)! prev = prev.next! if curr === prev { return...

View Article

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

I'm not sure whether this answer is applicable to Java, however I still think it belongs here:Whenever we are working with pointers on modern architectures we can expect them to be CPU word aligned....

View Article

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

If the linked list structure implements java.util.List. We can use the list size to keep track of our position in the list.We can traverse the nodes comparing our current position to the last node's...

View Article



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

I read through some answers and people have missed one obvious solution to the above problem.If given we can change the structure of the class Node then we can add a boolean flag to know if it has been...

View Article
Browsing latest articles
Browse All 30 View Live




Latest Images