Professional Documents
Culture Documents
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the
linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and
corresponding nodes contain the same data. Either head pointer given may be null meaning that the
corresponding list is empty.
PROGRAM:
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
struct SinglyLinkedListNode {
int data;
SinglyLinkedListNode* next;
};
struct SinglyLinkedList {
SinglyLinkedListNode* head;
SinglyLinkedListNode* tail;
};
node->data = node_data;
node->next = NULL;
return node;
if (!(*singly_linked_list)->head) {
(*singly_linked_list)->head = node;
} else {
(*singly_linked_list)->tail->next = node;
(*singly_linked_list)->tail = node;
}
void print_singly_linked_list(SinglyLinkedListNode* node, char* sep, FILE* fptr) {
while (node) {
node = node->next;
if (node) {
while (node) {
node = node->next;
free(temp);
/*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*/
while((head1!=NULL)&&(head2!=NULL)){
if(head1->data!=head2->data)
return 0;
head1=head1->next;
head2=head2->next;
if((head1!=NULL&&head2==NULL)||(head1==NULL&&head2!=NULL))
return 0;
else
return 1;
int main()
INPUT:
2
2
SAMPLE OUTPUT:
OUTPUT:
Compiler output:
SUCCESS.
AIM:
You’re given the pointer to the head nodes of two sorted linked lists. The data in both lists will be sorted
in ascending order. Change the next pointers to obtain a single, merged linked list which also has data in
ascending order. Either head pointer given may be null meaning that the corresponding list is empty.
PROGRAM:
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
struct SinglyLinkedListNode {
int data;
SinglyLinkedListNode* next;
};
struct SinglyLinkedList {
SinglyLinkedListNode* head;
SinglyLinkedListNode* tail;
};
node->next = NULL;
return node;
if (!(*singly_linked_list)->head) {
(*singly_linked_list)->head = node;
} else {
(*singly_linked_list)->tail->next = node;
(*singly_linked_list)->tail = node;
while (node) {
node = node->next;
if (node) {
fprintf(fptr, "%s", sep);
while (node) {
node = node->next;
free(temp);
/*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*/
SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
if(head1==NULL&&head2==NULL)
return NULL;
if(head1==NULL)
return head2;
if(head2==NULL)
return head1;
if(head1->data<head2->data)
head1->next=mergeLists(head1->next,head2);
return head1;
else
head2->next=mergeLists(head1,head2->next);
return head2;
int main()
INPUT:
1
SAMPLE OUTPUT:
12334
OUTPUT:
COMPILER OUTPUT:
SUCCESS
AIM:
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 may be null indicating that the list is empty.
PROGRAM:
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
struct SinglyLinkedListNode {
int data;
SinglyLinkedListNode* next;
};
struct SinglyLinkedList {
SinglyLinkedListNode* head;
SinglyLinkedListNode* tail;
};
node->data = node_data;
node->next = NULL;
return node;
}
if (!(*singly_linked_list)->head) {
(*singly_linked_list)->head = node;
} else {
(*singly_linked_list)->tail->next = node;
(*singly_linked_list)->tail = node;
while (node) {
node = node->next;
if (node) {
}
void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
node = node->next;
free(temp);
/*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*/
SinglyLinkedListNode *p=NULL;
p=head;
if(head==NULL)
return NULL;
while(head->next!=NULL){
if(head->data==head->next->data)
head->next=head->next->next;
else
head=head->next;
return p;
int main()
INPUT:
SAMPLE OUTPUT:
1234
OUTPUT:
COMPILER OUTPUT:
SUCCESS
4.CYCLE DETECTION
AIM:
A linked list is said to contain a cycle if any node is visited more than once while traversing the
list.Complete the function provided for you in your editor. It has one parameter: a pointer to a Node
object named HEAD that points to the head of a linked list. Your function must return a boolean
denoting whether or not there is a cycle in the list. If there is a cycle, return true; otherwise, return
false.Note: If the list is empty, HEAD will be null.
PROGRAM:
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
struct SinglyLinkedListNode {
int data;
SinglyLinkedListNode* next;
};
struct SinglyLinkedList {
SinglyLinkedListNode* head;
SinglyLinkedListNode* tail;
};
node->data = node_data;
node->next = NULL;
return node;
if (!(*singly_linked_list)->head) {
(*singly_linked_list)->head = node;
} else {
(*singly_linked_list)->tail->next = node;
(*singly_linked_list)->tail = node;
}
while (node) {
node = node->next;
if (node) {
while (node) {
node = node->next;
free(temp);
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*/
SinglyLinkedListNode *t1=NULL,*t2=NULL;
t1=head;
t2=head;
while((t2!=NULL)&&(t2->next!=NULL))
t1=t1->next;
t2=t2->next->next;
if(t1==t2)
return 1;
return 0;
int main()
INPUT:
SAMPLE OUTPUT:
OUTPUT:
COMPILER OUTPUT:
SUCCESS
AIM:
Given pointers to the head nodes of linked lists that merge together at some point, find the Node where
the two lists merge. It is guaranteed that the two head Nodes will be different, and neither will be
NULL.In the diagram below, the two lists converge at Node x:
x--->y--->z--->NULL
PROGRAM:
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
struct SinglyLinkedListNode {
int data;
SinglyLinkedListNode* next;
};
struct SinglyLinkedList {
SinglyLinkedListNode* head;
SinglyLinkedListNode* tail;
};
node->data = node_data;
node->next = NULL;
return node;
if (!(*singly_linked_list)->head) {
(*singly_linked_list)->head = node;
} else {
(*singly_linked_list)->tail->next = node;
(*singly_linked_list)->tail = node;
while (node) {
node = node->next;
if (node) {
while (node) {
node = node->next;
free(temp);
/*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
SinglyLinkedListNode *t=NULL;
while(head1!=NULL)
t=head2;
while(t!=NULL)
if(t==head1)
return t->data;
t=t->next;
head1=head1->next;
return head1->data;
int main()
INPUT:
The diagrams below are graphical representations of the lists that input Nodes HEADA and HEADB are
connected to. Recall that this is a method-only challenge; the method only has initial visibility to those 2
Nodes and must explore the rest of the Nodes using some algorithm of your own design.
Test Case 0
1
2--->3--->NULL
Test Case 1
1--->2
3--->Null
SAMPLE OUTPUT:
OUTPUT:
COMPILER OUTPUT:
SUCCESS