Skip to main content

How would you find a cycle in a linked list? Try to do it in O(n) time. Try it using a constant amount of memory.

There are quite a few ways to solving this problem. Here are a couple of them:
If you have access to the code, add a new field "visited" to the node structure and initialize it with 0.
typedef struct {
int visited;
...Other fields..
}
Now, start traversing through the linked-list checking for the value of this visited field. If the value
is set to 0, set it to 1 and go to the next node in the list. If you find a node with its visited value set
to 1 before you reach the end of the list, there is a cycle in the linked-list. If not, there is no cycle
present in the list. As you can see this takes O (n) time but requires variable amount of memory.
To accomplish this task using constant memory is much more challenging at first. If you like simple
yet challenging problems, try it yourself before looking at the answer below.
bool cycle_finder(Node *head)
{
Node *p1, *p2;
bool hasCycle = false;
p1 = p2 = head;
while (p2 && p2->next) {
p1 = p1->next;
p2 = p2->next->next;
if (p1 == p2) {
hasCycle = true;
break;
}
}
return hasCycle;
}
Note that p2 is moving through the list twice as fast as p1. If the cycle exists in the list, p1 and p2
will eventually be equal. Otherwise, the loop will terminate as soon as p2 reaches the end of the list.

Comments

Popular posts from this blog

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into ...

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checkin...