Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Chapter2-LinkedLists/2.6-FindLoop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Problem: Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.
*/

//built on top of linked list in data structures
LinkedList.prototype.findLoop = function() {
var n = this.head;
var storedValues = [];
var res = null;
while (n) {
if (storedValues.indexOf(n.data) < 0) {
storedValues.push(n.data);
n = n.next;
} else {
res = n.data;
break;
}
}
return res;
};

//runtime: n^2