CA1
CA1
CA1
Answer:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
printf("List:\n");
printList(head);
printf("Length of the list: %d\n", lengthOfList(head));
return 0;
}
Question: Write a function to find the middle node of a singly linked list.
Answer:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
return slow;
}
printf("List:\n");
printList(head);
return 0;
}
Question: Write a function to delete a node at a specific position in a singly linked list.
Answer:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
if (position == 0) {
*head_ref = temp->next; // Change head
free(temp); // Free old head
return;
}
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
printf("Original list:\n");
printList(head);
deleteNode(&head, 2);
return 0;
}
Answer:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node {
int data;
struct Node* next;
};
second_half = slow;
prev_of_slow->next = NULL;
prev = NULL;
current = second_half;
second_half = prev;
struct Node* first_half = head;
return true;
}
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(2);
head->next->next->next->next = createNode(1);
printf("List:\n");
printList(head);
if (isPalindrome(head)) {
printf("The list is a palindrome.\n");
} else {
printf("The list is not a palindrome.\n");
}
return 0;
}
Question: Write a function to merge two sorted linked lists into a single sorted list.
Answer:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
if (mergedHead == NULL) {
mergedHead = temp;
mergedTail = temp;
} else {
mergedTail->next = temp;
mergedTail = temp;
}
}
return mergedHead;
}
int main() {
struct Node* head1 = createNode(1);
head1->next = createNode(3);
head1->next->next = createNode(5);
printf("List 1:\n");
printList(head1);
printf("List 2:\n");
printList(head2);
printf("Merged list:\n");
printList(mergedHead);
return 0;
}
Answer:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
head = prev;
return head;
}
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
printf("Original list:\n");
printList(head);
head = reverseList(head);
printf("Reversed list:\n");
printList(head);
return 0;
}
Answer:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node {
int data;
struct Node* next;
};
if (slow == fast) {
return true;
}
}
return false;
}
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
if (hasCycle(head)) {
printf("The list has a cycle.\n");
} else {
printf("The list does not have a cycle.\n");
}
return 0;
}
8. Find the nth Node from the End
Question: Write a function to find the nth node from the end of a singly linked list.
Answer:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int count = 0;
if (head != NULL) {
while (count < n) {
if (ref_ptr == NULL) {
printf("%d is greater than the number of nodes in the list.\n", n);
return NULL;
}
ref_ptr = ref_ptr->next;
count++;
}
return main_ptr;
}
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
printf("List:\n");
printList(head);
int n = 2;
struct Node* nthNode = findNthFromEnd(head, n);
if (nthNode) {
printf("The %dth node from the end is %d\n", n, nthNode->data);
}
return 0;
}
Question: Write a function to remove duplicates from a sorted singly linked list.
Answer:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = createNode(1);
head->next = createNode(1);
head->next->next = createNode(2);
head->next->next->next = createNode(3);
head->next->next->next->next = createNode(3);
printf("Original list:\n");
printList(head);
removeDuplicates(head);
return 0;
}
Question: Write a function to swap nodes in a singly linked list without swapping their data.
Answer:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next
->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
printf("Original list:\n");
printList(head);
int x = 2, y = 4;
swapNodes(&head, x, y);
return 0;
}
These questions and answers should provide a solid understanding of common operations and
techniques for working with pointers and singly linked lists in C.