C
C#11mo ago
yuval

Need help with question about lists

We say that a list "contains a circle" if there is a link in it that points to the link that appeared before it in the list. For example the following list contains a circle: Write an action called HasCircle that receives a list of integers. The operation will return an answer as to whether the received list contains a circle.
31 Replies
Jimmacle
Jimmacle11mo ago
what does your code look like so far?
yuval
yuvalOP11mo ago
public static bool HasCircle(Node<int> lst) { Node<int> curr = lst; if(curr == null) return false; if (CircleSize(lst) == 1 && curr.GetNext() == lst) return true; if (CircleSize(lst) == 1 && !curr.HasNext()) return false; bool flag = false; Node<int> temp = lst.GetNext(); while(curr != null && !flag) { while(temp != null && !flag) { if (temp.GetNext() == curr) flag = true; temp = temp.GetNext(); } curr = curr.GetNext(); temp = curr.GetNext(); if (curr.HasNext()) temp = temp.GetNext(); else curr = curr.GetNext(); } return flag; }
Jimmacle
Jimmacle11mo ago
$code
MODiX
MODiX11mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
yuval
yuvalOP11mo ago
sry i dont know what to do
Angius
Angius11mo ago
No description
yuval
yuvalOP11mo ago
public static bool HasCircle(Node<int> lst)
{
Node<int> curr = lst;
if(curr == null)
return false;
if (CircleSize(lst) == 1 && curr.GetNext() == lst)
return true;
if (CircleSize(lst) == 1 && !curr.HasNext())
return false;
bool flag = false;
Node<int> temp = lst.GetNext();
while(curr != null && !flag)
{
while(temp != null && !flag)
{
if (temp.GetNext() == curr)
flag = true;
temp = temp.GetNext();
}
curr = curr.GetNext();
temp = curr.GetNext();
if (curr.HasNext())
temp = temp.GetNext();
else
curr = curr.GetNext();
}
return flag;
}
public static bool HasCircle(Node<int> lst)
{
Node<int> curr = lst;
if(curr == null)
return false;
if (CircleSize(lst) == 1 && curr.GetNext() == lst)
return true;
if (CircleSize(lst) == 1 && !curr.HasNext())
return false;
bool flag = false;
Node<int> temp = lst.GetNext();
while(curr != null && !flag)
{
while(temp != null && !flag)
{
if (temp.GetNext() == curr)
flag = true;
temp = temp.GetNext();
}
curr = curr.GetNext();
temp = curr.GetNext();
if (curr.HasNext())
temp = temp.GetNext();
else
curr = curr.GetNext();
}
return flag;
}
yall have an idea pls?
Jimmacle
Jimmacle11mo ago
so the goal is just to detect a cycle in a linked list?
yuval
yuvalOP11mo ago
yes
yuval
yuvalOP11mo ago
No description
yuval
yuvalOP11mo ago
this is an example i was given ?
Jimmacle
Jimmacle11mo ago
what do you think you'd need to detect if that happened?
yuval
yuvalOP11mo ago
wdym? i need to return true if it has an inner circled list
Jimmacle
Jimmacle11mo ago
yes, so what do you think would be useful to help you find that out?
yuval
yuvalOP11mo ago
i was thinking going over the list checking every cell to check if its connected to the first then the second, third and so on sry i took time to respond ty for helping
Jimmacle
Jimmacle11mo ago
sounds like something that could work, did you try it?
yuval
yuvalOP11mo ago
yes i have trouble with writing the code itself bc lists is pretty new to me
Jimmacle
Jimmacle11mo ago
to clarify, this isn't something a C# programmer would call a "list" we have an actual List<T> type, this would be referred to as a linked list
yuval
yuvalOP11mo ago
oh really oh so a node?
Jimmacle
Jimmacle11mo ago
a linked list
yuval
yuvalOP11mo ago
ok sry mb
Jimmacle
Jimmacle11mo ago
a linked list is a collection of nodes where each node contains a value and a reference to the next node (which is what you have)
yuval
yuvalOP11mo ago
yes this is what i meant english is not my first language so my translation is poor
Jimmacle
Jimmacle11mo ago
np, just wanted to avoid any confusion
yuval
yuvalOP11mo ago
ok i understand now so where do i start to check that there is a circled linked list inside a linked list? maybe i just overcomplicated the code but idk how to put what i think into the code
Jimmacle
Jimmacle11mo ago
the nested loops solution you suggested sounds like something that would work so you'd loop over every node in your list, then inside that loop check every node up to the current node and see if your "current" node points to it
yuval
yuvalOP11mo ago
can it pointto itself? does it matter?
Jimmacle
Jimmacle11mo ago
that's up to your project's requirements i would consider a node pointing to itself to be a cycle personally
yuval
yuvalOP11mo ago
yeah i think it is does it affect the code?
Jimmacle
Jimmacle11mo ago
it would, yes it would change where you stop checking in your inner loop
yuval
yuvalOP11mo ago
ok ill try
public static bool HasCircle(Node<int> lst)
{
Node<int> curr = lst;
if(curr == null)
return false;
bool flag = false;
Node<int> temp = lst;
while(curr != null && !flag)
{
while (temp != null && !flag)
{
if (temp.GetNext() == curr)
flag = true;
temp = temp.GetNext();
}
curr = curr.GetNext();
temp = curr;
}
return flag;
}
public static bool HasCircle(Node<int> lst)
{
Node<int> curr = lst;
if(curr == null)
return false;
bool flag = false;
Node<int> temp = lst;
while(curr != null && !flag)
{
while (temp != null && !flag)
{
if (temp.GetNext() == curr)
flag = true;
temp = temp.GetNext();
}
curr = curr.GetNext();
temp = curr;
}
return flag;
}
i changed it abit and it doesnt work in a case that its in the middle or after
Want results from more Discord servers?
Add your server