class Node{
constructor(data){
this
.data = data;
this
.next =
null
;
}
}
function
insertNode(head, tail, d){
if
(head ==
null
){
let newNode =
new
Node(d);
head = newNode;
tail = newNode;
newNode.next = newNode;
}
else
{
let temp =
new
Node(d);
temp.next = tail.next;
tail.next = temp;
tail = tail.next;
}
return
[head, tail];
}
function
print(head){
let curr = head;
if
(head ==
null
){
console.log(
"List is Empty."
);
return
;
}
do
{
console.log(curr.data +
" "
);
curr = curr.next;
}
while
(curr != head);
console.log(
""
);
}
function
convertString(head){
let curr = head;
let s =
""
;
if
(head ==
null
)
return
""
;
do
{
s += (curr.data).toString();
s +=
"*"
;
curr = curr.next;
}
while
(curr != head);
return
s;
}
function
checkIdentical(S, T){
if
(S.length != T.length)
return
false
;
let temp = S+S;
if
(temp.includes(T))
return
true
;
else
return
false
;
}
let head1 =
null
;
let tail1 =
null
;
[head1, tail1] = insertNode(head1, tail1, 1);
[head1, tail1] = insertNode(head1, tail1, 2);
[head1, tail1] = insertNode(head1, tail1, 3);
[head1, tail1] = insertNode(head1, tail1, 4);
[head1, tail1] = insertNode(head1, tail1, 5);
[head1, tail1] = insertNode(head1, tail1, 1);
[head1, tail1] = insertNode(head1, tail1, 2);
[head1, tail1] = insertNode(head1, tail1, 6);
let head2 =
null
;
let tail2 =
null
;
[head2, tail2] = insertNode(head2, tail2, 5);
[head2, tail2] = insertNode(head2, tail2, 1);
[head2, tail2] = insertNode(head2, tail2, 2);
[head2, tail2] = insertNode(head2, tail2, 6);
[head2, tail2] = insertNode(head2, tail2, 1);
[head2, tail2] = insertNode(head2, tail2, 2);
[head2, tail2] = insertNode(head2, tail2, 3);
[head2, tail2] = insertNode(head2, tail2, 4);
console.log(
"First circular linked list : "
);
print(head1);
console.log(
"Second circular linked list : "
);
print(head2);
let S = convertString(head1);
let T = convertString(head2);
let flag = checkIdentical(S, T);
if
(flag) console.log(
"Identical"
);
else
console.log(
"Not Identical"
);