You are on page 1of 22

In-lab Task:

1. You are given the pointer to the head node of a linked list and an integer to add to the
list. Create a new node with the given integer. Insert this node at the tail of the linked
list and return the head node of the linked list formed after inserting this new node. The
given head pointer may be null, meaning that the initial list is empty.
Link: https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-
list/problem
Sol:
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {

SinglyLinkedListNode *n=(SinglyLinkedListNode *)malloc(sizeof(SinglyLinkedListNode));

n->data=data;

n->next=NULL;

if(head==NULL){

head=n;

else {

SinglyLinkedListNode *t=head;

while(t->next)

t=t->next;

t->next=n;

return head;

2. This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print
each node's data element, one per line. If the head pointer is null (indicating the list is empty), there
is nothing to print.

Link: https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem

Sol:
void printLinkedList(SinglyLinkedListNode* head) {

SinglyLinkedListNode *n=head;

while(n){

printf("%d\n",n->data);

n=n->next;
}

3. Reversed Linked List

You are given a linked list that contains N integers, you have to print the elements of linked list in the
reverse order.

Input format

• First line: N

• Next line: N space-separated integers that denote elements of the Linked list.

Output format

Print the N elements of the original list.

Link: https://www.hackerrank.com/contests/ds-week3/challenges/reversed-linked-list-3

Sol:

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* insertAtEnd(struct Node* head, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

if (head == NULL) {

return newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL) {


temp = temp->next;

temp->next = newNode;

return head;

void printReverse(struct Node* head) {

if (head == NULL) {

return;

printReverse(head->next);

printf("%d ", head->data);

int main() {

int N, data;

scanf("%d", &N);

struct Node* head = NULL;

for (int i = 0; i < N; i++) {

scanf("%d", &data);

head = insertAtEnd(head, data);

printReverse(head);

return 0;

Post-lab Task:

1.Implement a singly linked list having all unique elements with the following operations.

I 0 x – Inserts element x at the end.

I 1 y x – If the element y exists, then insert element x after the element y, else insert element y
before the existing element x. Assuming either the element x or the element y exists.

I 2 z y x – Inserts element x in the middle of the elements z and y. The element z appears before the
element y.

U x p – Links the next pointer of the element x to the node lying at the pth position from the element
x while traversing towards right. In case of insufficient number of nodes, the counting continues by
updating the existing linked list to its circular version.

Link: https://www.codechef.com/PRACTICE/problems-old/MIDLE003

Solution:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct scorel

int no;

struct scorel *rp;

};

struct namel

char a[10];

struct scorel *rp;

struct namel *dp;

};

void main()

int i,j,n,m;

struct namel temp,*head,*end,*s;

struct scorel *tmp,*had,*nd;

clrscr();

printf("enter no of students");

scanf("%d",&n);

end=NULL;

for(i=0;i<n;i++)

temp=(struct namel*)malloc(sizeof(struct namel));


printf("enter name\n");

scanf("%s",temp->a);

temp->rp=NULL;

temp->dp=NULL;

*(s+i)=temp;

if(end==NULL)

end=head=temp;

else

end->dp=temp;

end=temp;

printf("enter no of scores");

scanf("%d",&m);

nd=NULL;

for(j=0;j<m;j++)

tmp=(struct scorel*)malloc(sizeof(struct scorel));

printf("enter score\n");

scanf("%d",&tmp->no);

tmp->rp=NULL;

if(nd==NULL)

nd=had=tmp;

temp->rp=tmp;

else

nd->rp=tmp;
nd=tmp;

for(i=0;i<n;i++)

temp=*(s+i);

printf("%s-->",temp->a);

tmp=temp->rp;

while(tmp!=NULL)

printf("%d-->",tmp->no);

tmp=tmp->rp;

printf("\n");

getch();

}
2.Given the head of a linked list, Find the number of critical points. (The starting and end are not
considered critical points). Local minima or maxima are called critical points. A Node is called a local
minimum if both next and previous elements are greater than the current element. A Node is called
a local maximum if both next and previous elements are smaller than the current element.

Constraints

• 1≤ Number of elements in the linked list, N ≤105

1. 1≤Node.data≤109

Link: https://www.codechef.com/practice/course/linked-lists/LINKLISTF/problems/CRITLIST

Solution:

#include <stdio.h>

#include <stdlib.h>

// Node structure for the linked list

struct Node {

int data;

struct Node* next;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;
return newNode;

// Function to count critical points in the linked list

int countCriticalPoints(struct Node* head) {

if (head == NULL || head->next == NULL) {

// No critical points if the linked list has less than 3 nodes

return 0;

int criticalPoints = 0;

struct Node* current = head->next;

// Traverse the linked list from the second node to the second-to-last node

while (current->next != NULL) {

if ((current->data > current->next->data && current->data > head->data) ||

(current->data < current->next->data && current->data < head->data)) {

// Node is a local minima or maxima

criticalPoints++;

head = head->next;

current = current->next;

return criticalPoints;

int main() {

int N;

scanf("%d", &N);
struct Node* head = NULL;

struct Node* tail = NULL;

// Read input elements and create linked list

for (int i = 0; i < N; i++) {

int data;

scanf("%d", &data);

struct Node* newNode = createNode(data);

if (head == NULL) {

head = newNode;

tail = newNode;

} else {

tail->next = newNode;

tail = newNode;

int result = countCriticalPoints(head);

printf("%d\n", result);

return 0;

Skill - Session:

1.You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in
ascending

order. Delete nodes and return a sorted list with each distinct value in the original list. The given
head pointer

may be null indicating that the list is empty.

Example
head refers to the first node in the list 1→2 →2→3→3→3→3→ NULL.

Remove 1 of the 2 data values and return head pointing to the revised list 1→2 →3 NULL.

Link: https://www.hackerrank.com/challenges/delete-duplicate-value-nodes-from-a-sorted-linked-
list/problem

Solution: -

SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* llist) {

SinglyLinkedListNode *t=llist, *t1;

t1=t->next;

while(t1)

if(t->data==t1->data)

t->next=t1->next;

t1=t->next;

else {

t=t->next;

t1=t->next;

return llist;

2. You are given a increasing order sorted singly linked list. You should find the minimum integer in
the list which is greater than or equal to x.

More formally, there is a singly liked list built on an array of n elements. Element with index i
contains two integers: value i is the integer value in this element, and next i that is the index of the
next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if
next i ≠ - 1, then value next i > value i.
Link: https://codeforces.com/problemset/problem/843/B

Solution:
#include <stdio.h>

// Function to find the lower bound of a target value in a sorted array

int lowerBound(int arr[], int n, int target) {

int low = 0, high = n - 1, result = -1;

while (low <= high) {

int mid = low + (high - low) / 2;

if (arr[mid] >= target) {

result = mid;

high = mid - 1;

} else {

low = mid + 1;

return result;

int main() {

int n;

// Input the size of the array

printf("Enter the size of the array: ");

scanf("%d", &n);

int arr[n];
// Input the sorted array

printf("Enter the sorted array elements:\n");

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

int target;

// Input the target value

printf("Enter the target value: ");

scanf("%d", &target);

// Find the lower bound and print the result

int result = lowerBound(arr, n, target);

if (result != -1) {

printf("Lower bound of %d is at index %d\n", target, result);

} else {

printf("No element in the array is greater than or equal to %d\n", target);

return 0;

3. Design your implementation of the linked list. You can choose to use a singly or doubly linked list.A
node in a singly linked list should have two attributes: val and next. val is the value of the current
node, and next is a pointer/reference to the next node.If you want to use the doubly linked list, you
will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in
the linked list are 0-indexed.

Link: https://leetcode.com/problems/design-linked-list/description/

Program: -

#include <stdio.h>

#include <stdlib.h>
struct ListNode {

int val;

struct ListNode* next;

};

struct MyLinkedList {

struct ListNode dummy;

int length;

};

struct ListNode* createNode(int x) {

struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));

node->val = x;

node->next = NULL;

return node;

void initialize(struct MyLinkedList* obj) {

obj->dummy.next = NULL;

obj->length = 0;

int get(struct MyLinkedList* obj, int index) {

if (index < 0 || index >= obj->length)

return -1;

struct ListNode* curr = obj->dummy.next;

for (int i = 0; i < index; ++i)

curr = curr->next;
return curr->val;

void addAtHead(struct MyLinkedList* obj, int val) {

struct ListNode* head = obj->dummy.next;

struct ListNode* node = createNode(val);

node->next = head;

obj->dummy.next = node;

++obj->length;

void addAtTail(struct MyLinkedList* obj, int val) {

struct ListNode* curr = &(obj->dummy);

while (curr->next)

curr = curr->next;

curr->next = createNode(val);

++obj->length;

void addAtIndex(struct MyLinkedList* obj, int index, int val) {

if (index > obj->length)

return;

struct ListNode* curr = &(obj->dummy);

for (int i = 0; i < index; ++i)

curr = curr->next;

struct ListNode* cache = curr->next;


struct ListNode* node = createNode(val);

node->next = cache;

curr->next = node;

++obj->length;

void deleteAtIndex(struct MyLinkedList* obj, int index) {

if (index < 0 || index >= obj->length)

return;

struct ListNode* curr = &(obj->dummy);

for (int i = 0; i < index; ++i)

curr = curr->next;

struct ListNode* cache = curr->next;

curr->next = cache->next;

--obj->length;

free(cache);

void printList(struct MyLinkedList* obj) {

struct ListNode* curr = obj->dummy.next;

while (curr) {

printf("%d -> ", curr->val);

curr = curr->next;

}
printf("NULL\n");

void freeList(struct MyLinkedList* obj) {

struct ListNode* curr = obj->dummy.next;

struct ListNode* next;

while (curr) {

next = curr->next;

free(curr);

curr = next;

obj->dummy.next = NULL;

obj->length = 0;

int main() {

struct MyLinkedList obj;

initialize(&obj);

addAtHead(&obj, 1);

addAtTail(&obj, 3);

addAtIndex(&obj, 1, 2);

printf("Linked List: ");

printList(&obj);

int valueAtIndex = get(&obj, 1);

printf("Value at index 1: %d\n", valueAtIndex);


deleteAtIndex(&obj, 1);

printf("Linked List after deletion at index 1: ");

printList(&obj);

freeList(&obj);

return 0;

4. Given the heads of two singly linked-lists headA and headB, return the node at which the two lists
intersect. If the two linked lists have no intersection at all, return null.

For example, the following two linked lists begin to intersect at node c1:

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

Link: https://leetcode.com/problems/intersection-of-two-linked-lists/description/

Program:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode
*headB) {

struct ListNode *t1=headA, *t2=headB;


while(t1)
{
t2=headB;
while(t2)
{
if(t1==t2)
return t2;
t2=t2->next;
}
t1=t1->next;
}
return NULL;
}
5.

#include <stdio.h>

#include <stdlib.h>

// Tree Node Structure

struct Node {

int data;

struct Node* next;

};

// Create new Node

struct Node* newLNode(int data) {

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

temp->data = data;

temp->next = NULL;

return temp;

// Function for finding midpoint recursively

void midpoint_util(struct Node* head, int* n, struct Node** mid) {

// If we reached the end of the linked list

if (head == NULL) {

*n = (*n) / 2;

return;

*n = *n + 1;

midpoint_util(head->next, n, mid);

// Rolling back, decrement n by one


*n = *n - 1;

if (*n == 0) {

// Final answer

*mid = head;

struct Node* midpoint(struct Node* head) {

struct Node* mid = NULL;

int n = 1;

midpoint_util(head, &n, &mid);

return mid;

int main() {

struct Node* head = newLNode(1);

head->next = newLNode(2);

head->next->next = newLNode(3);

head->next->next->next = newLNode(4);

head->next->next->next->next = newLNode(5);

struct Node* result = midpoint(head);

printf("%d\n", result->data);

// Free allocated memory

struct Node* current = head;

while (current != NULL) {

struct Node* next = current->next;

free(current);

current = next;

}
return 0;

6.Starting from the second node delete all alternate nodes.

Given a Singly Linked List, starting from the second node delete all alternate nodes of it.

Sample Input

10 20 30 40 50 None

Sample Output

10 30 50 None

Link: https://www.hackerrank.com/contests/ds-week3/challenges/starting-from-the-second-node-
delete-allalternate-nodes

Program: -

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void deleteAlternateNodes(struct Node* head) {

struct Node* current = head;

struct Node* nextNode;

while (current != NULL && current->next != NULL) {

nextNode = current->next;

current->next = nextNode->next;

free(nextNode);

current = current->next;
}

void printList(struct Node* head) {

while (head != NULL) {

printf("%d ", head->data);

head = head->next;

printf("None\n");

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

int main() {

int data;

struct Node* head = NULL;

struct Node* current;

while (1) {

scanf("%d", &data);

if (data == -1)

break;

if (head == NULL) {

head = createNode(data);
current = head;

} else {

current->next = createNode(data);

current = current->next;

deleteAlternateNodes(head);

printList(head);

while (head != NULL) {

struct Node* temp = head;

head = head->next;

free(temp);

return 0;

You might also like