Assignment 2 1484
Assignment 2 1484
(ADVANCED CODING)
Kishan das
VU21CSEN0101071
1. Reverse a linked list: Given a linked list, reverse the linked lst and return the
pointer to the reversed list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Helper func5on to create a new node struct ListNode* createNode(int value) { struct
ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); newNode-
>value = value; newNode->next = NULL;
return newNode;
}
int main() {
// Crea5ng the linked list 1 -> 2 -> 3 -> 4 -> NULL
struct ListNode* head = createNode(1); head>next
= createNode(2); head->next->next =
createNode(3);
head->next->next->next = createNode(4);
prinS("Original List:\n");
printList(head);
prinS("Reversed List:\n");
printList(reversedHead);
return 0;
}
Output:
2)Remove fullstops from a string: Given a string with
alphanumeric characters and fullstops, write a program
to remove the fullstops from the string.
Code:
#include <stdio.h>
#include <string.h>
return 0;
}
Output:
Code:
#include <stdio.h>
#include <stdlib.h>
return slow; // slow will be at the middle when fast reaches the
end
}
int main() {
// Creating the linked list 1 -> 2 -> 3 -> 4 -> 5 -> NULL struct
ListNode* 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("Linked List:\n"); printList(head);
return 0;
}
Output:
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
// Creating the linked list 1 -> 2 -> 3 -> 4 -> 5 ->
NULL
struct ListNode* 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);
return 0;
}
Output: