Solution and explanation
Complexity
| Question | Time | Extra space |
|---|
| 21. Find middle | O(n) | O(1) |
| 22. Reverse iteratively | O(n) | O(1) |
| 23. Reverse recursively | O(n) | O(n) call stack |
| 24. Detect cycle | O(n) | O(1) |
| 25. Find cycle start | O(n) | O(1) |
Expected output
Original: 10 -> 20 -> 30 -> 40 -> 50 -> NULL
Q21 Middle: 30
Q22 Reversed iteratively: 50 -> 40 -> 30 -> 20 -> 10 -> NULL
Q23 Reversed recursively: 10 -> 20 -> 30 -> 40 -> 50 -> NULL
Q24 Has cycle: Yes
Q25 Cycle starts at: 30
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node* createNode(int value) {
Node *n = malloc(sizeof(Node));
if (!n) exit(EXIT_FAILURE);
n->data = value;
n->next = NULL;
return n;
}
Node* insertEnd(Node *head, int value) {
Node *n = createNode(value);
if (!head) return n;
Node *cur = head;
while (cur->next) cur = cur->next;
cur->next = n;
return head;
}
void display(Node *head) {
while (head) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
// Q21. Find middle node (second middle for even length)
Node* findMiddle(Node *head) {
Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
// Q22. Reverse iteratively
Node* reverseIterative(Node *head) {
Node *prev = NULL, *cur = head;
while (cur) {
Node *next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
return prev;
}
// Q23. Reverse recursively
Node* reverseRecursive(Node *head) {
if (!head || !head->next) return head;
Node *newHead = reverseRecursive(head->next);
head->next->next = head;
head->next = NULL;
return newHead;
}
// Q24. Detect cycle using Floyd's algorithm
int hasCycle(Node *head) {
Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return 1;
}
return 0;
}
// Q25. Find the starting node of a cycle
Node* cycleStart(Node *head) {
Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
slow = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
}
return NULL;
}
void freeList(Node *head) {
while (head) {
Node *next = head->next;
free(head);
head = next;
}
}
int main(void) {
Node *head = NULL;
for (int i = 10; i <= 50; i += 10)
head = insertEnd(head, i);
printf("Original: ");
display(head);
Node *mid = findMiddle(head);
printf("Q21 Middle: %d\n", mid ? mid->data : -1);
Node *reversed = reverseIterative(head);
printf("Q22 Reversed iteratively: ");
display(reversed);
head = reverseRecursive(reversed);
printf("Q23 Reversed recursively: ");
display(head);
// Q24 and Q25: create a cycle from the last node to node 30.
Node *third = head->next->next;
Node *last = head;
while (last->next) last = last->next;
last->next = third;
printf("Q24 Has cycle: %s\n",
hasCycle(head) ? "Yes" : "No");
Node *start = cycleStart(head);
printf("Q25 Cycle starts at: %d\n",
start ? start->data : -1);
// Break the cycle before freeing the list.
last->next = NULL;
freeList(head);
return 0;
}
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
Node* insertEnd(Node* head, int value) {
Node* n = new Node(value);
if (!head) return n;
Node* cur = head;
while (cur->next) cur = cur->next;
cur->next = n;
return head;
}
void display(Node* head) {
while (head) {
cout << head->data << " -> ";
head = head->next;
}
cout << "NULL\n";
}
// Q21
Node* findMiddle(Node* head) {
Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
// Q22
Node* reverseIterative(Node* head) {
Node *prev = nullptr, *cur = head;
while (cur) {
Node* next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
return prev;
}
// Q23
Node* reverseRecursive(Node* head) {
if (!head || !head->next) return head;
Node* newHead = reverseRecursive(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
// Q24
bool hasCycle(Node* head) {
Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
// Q25
Node* cycleStart(Node* head) {
Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
slow = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
}
return nullptr;
}
void freeList(Node* head) {
while (head) {
Node* next = head->next;
delete head;
head = next;
}
}
int main() {
Node* head = nullptr;
for (int i = 10; i <= 50; i += 10)
head = insertEnd(head, i);
cout << "Original: ";
display(head);
cout << "Q21 Middle: " << findMiddle(head)->data << '\n';
Node* reversed = reverseIterative(head);
cout << "Q22 Reversed iteratively: ";
display(reversed);
head = reverseRecursive(reversed);
cout << "Q23 Reversed recursively: ";
display(head);
Node* third = head->next->next;
Node* last = head;
while (last->next) last = last->next;
last->next = third;
cout << "Q24 Has cycle: "
<< (hasCycle(head) ? "Yes" : "No") << '\n';
Node* start = cycleStart(head);
cout << "Q25 Cycle starts at: "
<< (start ? start->data : -1) << '\n';
last->next = nullptr;
freeList(head);
return 0;
}
Java
public class LinkedList21To25 {
static class Node {
int data;
Node next;
Node(int value) {
data = value;
}
}
static Node insertEnd(Node head, int value) {
Node n = new Node(value);
if (head == null) return n;
Node cur = head;
while (cur.next != null) cur = cur.next;
cur.next = n;
return head;
}
static void display(Node head) {
while (head != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.println("NULL");
}
// Q21
static Node findMiddle(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// Q22
static Node reverseIterative(Node head) {
Node prev = null, cur = head;
while (cur != null) {
Node next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
// Q23
static Node reverseRecursive(Node head) {
if (head == null || head.next == null) return head;
Node newHead = reverseRecursive(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
// Q24
static boolean hasCycle(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
// Q25
static Node cycleStart(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
public static void main(String[] args) {
Node head = null;
for (int i = 10; i <= 50; i += 10)
head = insertEnd(head, i);
System.out.print("Original: ");
display(head);
System.out.println("Q21 Middle: " + findMiddle(head).data);
Node reversed = reverseIterative(head);
System.out.print("Q22 Reversed iteratively: ");
display(reversed);
head = reverseRecursive(reversed);
System.out.print("Q23 Reversed recursively: ");
display(head);
Node third = head.next.next;
Node last = head;
while (last.next != null) last = last.next;
last.next = third;
System.out.println("Q24 Has cycle: " +
(hasCycle(head) ? "Yes" : "No"));
Node start = cycleStart(head);
System.out.println("Q25 Cycle starts at: " +
(start == null ? -1 : start.data));
// Break cycle so the list can be traversed safely afterward.
last.next = null;
}
}