DS Week 3 Solution
DS Week 3 Solution
DS Week 3 Solution
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) {
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;
}
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
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;
};
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
return newNode;
} else {
temp->next = newNode;
return head;
if (head == NULL) {
return;
printReverse(head->next);
int main() {
int N, data;
scanf("%d", &N);
scanf("%d", &data);
printReverse(head);
return 0;
Post-lab Task:
1.Implement a singly linked list having all unique elements with the following operations.
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 namel
char a[10];
};
void main()
int i,j,n,m;
clrscr();
printf("enter no of students");
scanf("%d",&n);
end=NULL;
for(i=0;i<n;i++)
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++)
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. 1≤Node.data≤109
Link: https://www.codechef.com/practice/course/linked-lists/LINKLISTF/problems/CRITLIST
Solution:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
return 0;
int criticalPoints = 0;
// Traverse the linked list from the second node to the second-to-last node
criticalPoints++;
head = head->next;
current = current->next;
return criticalPoints;
int main() {
int N;
scanf("%d", &N);
struct Node* head = NULL;
int data;
scanf("%d", &data);
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
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
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: -
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>
result = mid;
high = mid - 1;
} else {
low = mid + 1;
return result;
int main() {
int n;
scanf("%d", &n);
int arr[n];
// Input the sorted array
scanf("%d", &arr[i]);
int target;
scanf("%d", &target);
if (result != -1) {
} else {
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 MyLinkedList {
int length;
};
node->val = x;
node->next = NULL;
return node;
obj->dummy.next = NULL;
obj->length = 0;
return -1;
curr = curr->next;
return curr->val;
node->next = head;
obj->dummy.next = node;
++obj->length;
while (curr->next)
curr = curr->next;
curr->next = createNode(val);
++obj->length;
return;
curr = curr->next;
node->next = cache;
curr->next = node;
++obj->length;
return;
curr = curr->next;
curr->next = cache->next;
--obj->length;
free(cache);
while (curr) {
curr = curr->next;
}
printf("NULL\n");
while (curr) {
next = curr->next;
free(curr);
curr = next;
obj->dummy.next = NULL;
obj->length = 0;
int main() {
initialize(&obj);
addAtHead(&obj, 1);
addAtTail(&obj, 3);
addAtIndex(&obj, 1, 2);
printList(&obj);
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) {
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
temp->data = data;
temp->next = NULL;
return temp;
if (head == NULL) {
*n = (*n) / 2;
return;
*n = *n + 1;
midpoint_util(head->next, n, mid);
if (*n == 0) {
// Final answer
*mid = head;
int n = 1;
return mid;
int main() {
head->next = newLNode(2);
head->next->next = newLNode(3);
head->next->next->next = newLNode(4);
head->next->next->next->next = newLNode(5);
printf("%d\n", result->data);
free(current);
current = next;
}
return 0;
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;
};
nextNode = current->next;
current->next = nextNode->next;
free(nextNode);
current = current->next;
}
head = head->next;
printf("None\n");
newNode->data = data;
newNode->next = NULL;
return newNode;
int main() {
int data;
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);
head = head->next;
free(temp);
return 0;