Write a C program to detect if a loop exists in a singly linked list using Floyd’s Cycle-Finding Algorithm (print 1 if loop exists, 0 otherwise).
First line contains n. Second line contains n elements. Third line contains loop position index (-1 if no loop, or 0-indexed position where last node connects).
Print 1 if loop is detected, else 0.
1 <= n <= 50
5 1 2 3 4 5 1
1
Use slow and fast pointers. If they meet, a cycle exists; if fast reaches NULL, no cycle exists.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
int n;
if (scanf("%d", &n) == 1) {
struct Node *head = NULL, *tail = NULL;
struct Node *loopNode = NULL;
for (int i = 0; i < n; i++) {
int val;
scanf("%d", &val);
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
if (i == 1) loopNode = newNode; // example tracking for test hooks
}
int loopPos;
scanf("%d", &loopPos);
// Write your code here to construct loop if loopPos >= 0 and detect loop
}
return 0;
}Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used