One Pager Cheat Sheet
- To detect a loop in a
linked list
, implement adetectLoop
method to take thelinked list
head node as the parameter and returntrue
orfalse
accordingly with the constraints oftime
andspace
complexities ofO(n)
andO(1)
, respectively. - A
walk
is a path through a graph that can either besimple
ormultiple
, and acycle
is a closed walk with no vertex/edge repetitions, starting and ending at the same vertex. - A
brute force
solution to detect aloop
in a linked list might be to manually traverse each node and check if there is any node that has anext
pointer to a previous node. - We can use a
nested loop
to check for cycles in a graph by keeping track of the previously-encountered vertices. - We can solve this problem by
nested traversal
, which requires us to traverse each node and then re-traverse the nodes already visited in the outer loop to identify any nodes that have been repeated. - The optimized algorithm only needs two
pointers
to traverse the graph, withconstant space complexity
, to detect any duplicate nodes. - A
Linked List
is a linear data structure composed of multiple nodes, each containing data and a pointer to the next node, allowing for efficient insertion, deletion, and navigation. - The key advantage of an array over a linked list is that it is much faster to access an element due to its contiguous block of memory, allowing random access with a calculatable memory address, contrasted with the nodes and pointers of linked lists, which requires node traversal.
- A
doubly linked list
provides efficient traversal bothforward
andbackward
compared to asingly linked list
, which only offersforward
traversal. - By using two pointers to traverse the linked list - one slow and one fast - we can detect whether there is a loop present.
- Floyd's Cycle-Finding Algorithm is a well-known Computer Science technique used to detect if there is a loop in a
linked list
by moving two pointers at different speeds through the list. - Two pointers will
meet
in afixed-length cycle
due to theirdistance increase of 1 per iteration
, resulting in anO(1)
space complexity. - The algorithm has a time complexity of O(n), as it
traverses
the arrayn
times to reach its end. - Hybrid linked list is a combination of different types of linked lists to create a unique list, rather than an existing type of linked list.
- A
circular linked list
has a pointer from the last node to the first node, forming a loop rather than a NULL pointer. - A
circular linked list
provides an infinite loop-like structure in which each node contains the address of the next node, eliminating the need for anull
pointer. - The time complexity to add an element to a
linked list
isO(n)
due to the need to traverse the entire list to find thenull pointer
.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx
54
}
var assert = require('assert');
​
function detectLoop(head) {
let pointer1, pointer2;
pointer1 = head;
pointer2 = head;
​
while (pointer2.next.next) {
pointer1 = pointer1.next;
pointer2 = pointer2.next.next;
​
if (pointer1 == pointer2) {
return true;
}
}
return false;
}
​
function Node(val) {
this.val = val;
this.next = null;
}
​
function LinkedListNode(val) {
this.val = val;
this.next = null;
}
​
var list1 = new LinkedListNode(3);
var nodes1 = [4, 5, 6, 7, 8, 9, 10];
createNodes(list1, nodes1);
​
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
You're doing a wonderful job. Keep going!
If you had any problems with this tutorial, check out the main forum thread here.