DS Week 4 Solution
DS Week 4 Solution
DS Week 4 Solution
In-lab:
Link: https://leetcode.com/problems/middle-of-the-linked-list/description/
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
Example 1:
Example 2:
Constraints:
Program: -
Link: https://leetcode.com/problems/linked-list-cycle/description/
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by
continuously following the next pointer. Internally, pos is used to denote the index of the node
that tail's next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Example 1:
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:
Constraints:
Program: -
3. Remove Friends
After getting her PhD, Christie has become a celebrity at her university, and her
facebook profile is full of friend requests. Being the nice girl she is, Christie has
accepted all the requests. Now Kuldeep is jealous of all the attention she is
getting from other guys, so he asks her to delete some of the guys from her
friend list. To avoid a 'scene', Christie decides to remove some friends from her
friend list, since she knows the popularity of each of the friend she has, she uses
the following algorithm to delete a friend. Algorithm Delete(Friend):
DeleteFriend=false for i = 1 to Friend.length-1 if (Friend[i].popularity <
Friend[i+1].popularity) delete i th friend DeleteFriend=true break if(DeleteFriend
== false) delete the last friend
Input Format
First line contains T number of test cases. First line of each test case
contains N, the number of friends Christie currently has and K, the
number of friends Christie decides to delete. Next lines contains
popularity of her friends separated by space.
Constraints
1<=T<=1000
1<=N<=100000
0<=K< N
0<=popularity_of_friend<=100
Output Format
For each test case print N-K numbers which represent popularity of
Christie friend's after deleting K friends.
Sample Input 0
3
31
3 100 1
52
19 12 3 4 17
53
23 45 11 77 18
Sample Output 0
100 1
19 12 17
77 18
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int i, j;
int deleteFriend = 0;
n--;
deleteFriend = 1;
break;
if (!deleteFriend) {
n--;
printf("\n");
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, k;
int friends[n];
scanf("%d", &friends[i]);
deleteFriends(friends, n, k);
return 0;
OR
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int i, j;
n--;
deleteFriend = 1;
break;
if (!deleteFriend) {
n--;
printf("\n");
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, k;
int friends[n];
for (int i = 0; i < n; i++) {
scanf("%d", &friends[i]);
deleteFriends(friends, n, k);
return 0;
Post-Lab:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
if (prev_node == NULL) {
return;
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->data = new_data;
// Since the new node is going to be the last node, make next of it as NULL
new_node->next = NULL;
// If the linked list is empty, then make the new node as head
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
last = last->next;
last->next = new_node;
return;
*head_ref = temp->next;
free(temp);
return;
// Search for the key to be deleted, keep track of the previous node
prev = temp;
temp = temp->next;
if (temp == NULL)
return;
prev->next = temp->next;
free(temp);
}
// Function to print the linked list
node = node->next;
printf("\n");
int main() {
insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtEnd(&head, 3);
insertAfter(head->next, 4);
printList(head);
// Delete a node
deleteNodeByKey(&head, 3);
printList(head);
return 0;
AN respectively. You need to find the value of the middle element of the linked list.
The middle element of a linked list of length N is the ( [N/2]+1)-th element from the head of the list.
Input Format
The first line of the input contains a single integer T - the number of test cases. The description of T
test
cases follows.
The second line of each test case contains N space-separated integers 1, 2,…,AN.
Output Format
For each test case, the function you complete should return the value of the middle element of the
list.
Note: You need to complete the function get Middle Element to solve the problem.
Constraints
1≤T≤100
1≤N≤105
the sum of N over all test cases does not exceed 2 .105
Link:https://www.codechef.com/practice/course/interview-dsa/DSAPREP_08/problems/LLMID
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
}*head=NULL,*tail=NULL;
n->data=data;
n->next=NULL;
return n;
if(head==NULL)
head=n;
tail=n;
return;
tail->next=n;
tail=n;
if(head==NULL || head->next==NULL)
return head;
t1=t1->next;
t2=t2->next->next;
return t1;
int main() {
int t,n,x, c;
scanf("%d",&t);
while(t--)
head=NULL;
scanf("%d",&n);
while(n--)
scanf("%d",&x);
addAtEnd(x);
printf("%d\n",x->data);
return 0;
Skill - Session:
Sanad has a problem, he is a beginner to Linked Lists just like you and he needs your help.
Given a linked list fill the function printList() to print the elements of the list. You have to print each
element of the
Link: https://www.hackerrank.com/contests/ds-week3/challenges/print-linked-list-2-1
Program: -
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
current = current->next;
}
int main() {
int N;
scanf("%d", &N);
int value;
scanf("%d", &value);
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
printList(head);
// Free the memory allocated for the linked list nodes
temp = head;
head = head->next;
free(temp);
return 0;
Input:- A->B->C->D->E
Constraint :- You have only access to the node to be deleted (node C).
Output :- A->B->D->E
Link: https://www.hackerrank.com/contests/ds-week4-1/challenges/delete-node-in-linked-list
Program: -
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
// Function to insert a node at the end of the list
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
last = last->next;
last->next = new_node;
return;
return;
fast_ptr = fast_ptr->next->next;
prev_ptr = slow_ptr;
slow_ptr = slow_ptr->next;
prev_ptr->next = slow_ptr->next;
free(slow_ptr);
node = node->next;
printf("\n");
// Main function
int main() {
append(&head, 'A');
append(&head, 'B');
append(&head, 'C');
append(&head, 'D');
append(&head, 'E');
//printList(head);
deleteMiddle(&head);
printList(head);
return 0;
You're given the pointer to the head node of a sorted linked list, where the data in the nodes is in
ascending order.
Delete as few nodes as possible so that the list does not contain any value more than once. The given
head pointer
Link: https://www.hackerrank.com/contests/ds-week4-1/challenges/delete-duplicate-nodes-from-a-
linked-list
Program: -
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
};
exit(1);
newNode->data = data;
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
if (current == NULL) {
return;
}
while (current->next != NULL) {
if (current->data == current->next->data) {
nextNext = current->next->next;
free(current->next);
current->next = nextNext;
} else {
current = current->next;
temp = temp->next;
printf("\n");
temp = head;
head = head->next;
free(temp);
}
int main() {
// Example usage
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
deleteDuplicates(head);
printList(head);
freeList(head);
return 0;
Write a function that moves the last node to the front in a given Singly Linked List.
Sample 1:
Input: 1 → 2 → 3 → 4 → 5
Output: 5 → 1 → 2 → 3 → 4
Sample 2:
Input: 3 → 8 → 1 → 5 → 7 → 12
Output: 12 → 3 → 8 → 1 → 5 → 7
Link: https://www.hackerrank.com/contests/ds-week4-1/challenges/moves-the-node
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = new_data;
new_node->next = *head_ref;
// Move the head to point to the new node
*head_ref = new_node;
return;
second_last = last;
last = last->next;
second_last->next = NULL;
last->next = *head_ref;
*head_ref = last;
node = node->next;
printf("\n");
}
int main() {
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
moveLastToFront(&head);
printList(head);
return 0;
Given two lists sorted in increasing order, create and return a new list representing the intersection
of the two lists.
The new list should be made with its own memory — the original lists should not be changed.
Sample 1:
Output: 2 → 4 → 6.
The elements 2, 4, 6 are common in both the list so they appear in the intersection list.
Link: https://www.hackerrank.com/contests/ds-week4-1/challenges/create-and-return-a-new-list
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = new_data;
new_node->next = NULL;
// If the linked list is empty, make the new node as the head
if (*head_ref == NULL) {
*head_ref = new_node;
return;
// Otherwise, traverse to the last node and append the new node
last = last->next;
last->next = new_node;
if (head1->data == head2->data) {
append(tail, head1->data);
tail = &((*tail)->next);
head1 = head1->next;
head2 = head2->next;
head1 = head1->next;
} else {
head2 = head2->next;
return result;
node = node->next;
}
printf("\n");
int main() {
append(&head1, 1);
append(&head1, 2);
append(&head1, 3);
append(&head1, 4);
append(&head1, 6);
append(&head2, 2);
append(&head2, 4);
append(&head2, 6);
append(&head2, 8);
printList(result);
return 0;
}
6. Returns the value at the Nth node from the end
Given a Linked List and a number N, write a function that returns the value at the Nth node from the
end of the
Linked List.
Sample 1:
Input: 10 → 20 → 30 → 40 → 50 → None
N=2
Output: 40
Link: https://www.hackerrank.com/contests/ds-week4-1/challenges
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
// Function to get the Nth node from the end of the linked list
if (head == NULL)
return -1;
// Move the second pointer to the nth node from the beginning
if (second == NULL)
second = second->next;
// Move both pointers until the second pointer reaches the end
first = first->next;
second = second->next;
}
// At this point, the first pointer is at the nth node from the end
return first->data;
int main() {
push(&head, 50);
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
int n = 2;
printf("%d\n",getNthFromEnd(head, n));
return 0;