Takes in a linkedList and returns true if the circular loop starts anywhere in the list.
Example 1: (linkedList starts at n1
)
const n1 = { val: 'n1Val', };
const n2 = { val: 'n2Val', };
const n3 = { val: 'n3Val', };
n1['next'] = n2;
n2['next'] = n3;
n3['next'] = n2;
Input: n1
Output: true
because the circular loop started somewhere in the list.
Example 2: (linkedList starts at n1
)
const n1 = { val: 'n1Val', };
const n2 = { val: 'n2Val', };
const n3 = { val: 'n3Val', };
const n4 = { val: 'n4Val', };
n1['next'] = n2;
n2['next'] = n3;
n3['next'] = n4;
n4['next'] = n2;
Input: n1
Output: true
because the circular loop started somewhere in the list.
Example 3: (linkedList starts at n1
)
const n1 = { val: 'n1Val', };
const n2 = { val: 'n2Val', };
const n3 = { val: 'n3Val', };
n1['next'] = n2;
n2['next'] = n3;
Input: n1
Output: false
because the circular loop does not start somewhere in the list.