You are on page 1of 24

1.

COMPARE TWO LINKED LISTS


AIM:

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();

typedef struct SinglyLinkedListNode SinglyLinkedListNode;

typedef struct SinglyLinkedList SinglyLinkedList;

struct SinglyLinkedListNode {

int data;

SinglyLinkedListNode* next;

};

struct SinglyLinkedList {
SinglyLinkedListNode* head;

SinglyLinkedListNode* tail;

};

SinglyLinkedListNode* create_singly_linked_list_node(int node_data) {

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

node->data = node_data;

node->next = NULL;

return node;

void insert_node_into_singly_linked_list(SinglyLinkedList** singly_linked_list, int node_data) {

SinglyLinkedListNode* node = create_singly_linked_list_node(node_data);

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) {

fprintf(fptr, "%d", node->data);

node = node->next;

if (node) {

fprintf(fptr, "%s", sep);

void free_singly_linked_list(SinglyLinkedListNode* node) {

while (node) {

SinglyLinkedListNode* temp = node;

node = node->next;

free(temp);

// Complete the compare_lists function below.

/*

* For your reference:


*

* SinglyLinkedListNode {

* int data;

* SinglyLinkedListNode* next;

* };

*/

bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {

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.

2.MERGE TWO SORTED LINKED LISTS

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();

typedef struct SinglyLinkedListNode SinglyLinkedListNode;

typedef struct SinglyLinkedList SinglyLinkedList;

struct SinglyLinkedListNode {

int data;

SinglyLinkedListNode* next;

};

struct SinglyLinkedList {

SinglyLinkedListNode* head;

SinglyLinkedListNode* tail;

};

SinglyLinkedListNode* create_singly_linked_list_node(int node_data) {

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


node->data = node_data;

node->next = NULL;

return node;

void insert_node_into_singly_linked_list(SinglyLinkedList** singly_linked_list, int node_data) {

SinglyLinkedListNode* node = create_singly_linked_list_node(node_data);

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) {

fprintf(fptr, "%d", node->data);

node = node->next;

if (node) {
fprintf(fptr, "%s", sep);

void free_singly_linked_list(SinglyLinkedListNode* node) {

while (node) {

SinglyLinkedListNode* temp = node;

node = node->next;

free(temp);

// Complete the mergeLists function below.

/*

* For your reference:

* 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

3.DELETE DUPLICATE-VALUE NODES FROM SORTED LIST

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();

typedef struct SinglyLinkedListNode SinglyLinkedListNode;

typedef struct SinglyLinkedList SinglyLinkedList;

struct SinglyLinkedListNode {

int data;

SinglyLinkedListNode* next;

};

struct SinglyLinkedList {

SinglyLinkedListNode* head;

SinglyLinkedListNode* tail;

};

SinglyLinkedListNode* create_singly_linked_list_node(int node_data) {

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

node->data = node_data;

node->next = NULL;

return node;
}

void insert_node_into_singly_linked_list(SinglyLinkedList** singly_linked_list, int node_data) {

SinglyLinkedListNode* node = create_singly_linked_list_node(node_data);

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) {

fprintf(fptr, "%d", node->data);

node = node->next;

if (node) {

fprintf(fptr, "%s", sep);

}
void free_singly_linked_list(SinglyLinkedListNode* node) {

while (node) {

SinglyLinkedListNode* temp = node;

node = node->next;

free(temp);

// Complete the removeDuplicates function below.

/*

* For your reference:

* SinglyLinkedListNode {

* int data;

* SinglyLinkedListNode* next;

* };

*/

SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* head) {

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();

typedef struct SinglyLinkedListNode SinglyLinkedListNode;

typedef struct SinglyLinkedList SinglyLinkedList;

struct SinglyLinkedListNode {

int data;

SinglyLinkedListNode* next;

};
struct SinglyLinkedList {

SinglyLinkedListNode* head;

SinglyLinkedListNode* tail;

};

SinglyLinkedListNode* create_singly_linked_list_node(int node_data) {

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

node->data = node_data;

node->next = NULL;

return node;

void insert_node_into_singly_linked_list(SinglyLinkedList** singly_linked_list, int node_data) {

SinglyLinkedListNode* node = create_singly_linked_list_node(node_data);

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) {

fprintf(fptr, "%d", node->data);

node = node->next;

if (node) {

fprintf(fptr, "%s", sep);

void free_singly_linked_list(SinglyLinkedListNode* node) {

while (node) {

SinglyLinkedListNode* temp = node;

node = node->next;

free(temp);

// Complete the has_cycle function below.


/*

* For your reference:

* SinglyLinkedListNode {

* int data;

* SinglyLinkedListNode* next;

* };

*/

bool has_cycle(SinglyLinkedListNode* head) {

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:

The following linked lists are passed as arguments to your function:

SAMPLE OUTPUT:

OUTPUT:

COMPILER OUTPUT:

SUCCESS

5.FIND MERGE POINT OF TWO LISTS

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:

[List #1] a--->b--->c

x--->y--->z--->NULL

[List #2] p--->q

Complete the int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) method


so that it finds and returns the data value of the Node where the two lists merge.

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();

typedef struct SinglyLinkedListNode SinglyLinkedListNode;

typedef struct SinglyLinkedList SinglyLinkedList;

struct SinglyLinkedListNode {

int data;

SinglyLinkedListNode* next;

};

struct SinglyLinkedList {

SinglyLinkedListNode* head;

SinglyLinkedListNode* tail;

};

SinglyLinkedListNode* create_singly_linked_list_node(int node_data) {


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

node->data = node_data;

node->next = NULL;

return node;

void insert_node_into_singly_linked_list(SinglyLinkedList** singly_linked_list, int node_data) {

SinglyLinkedListNode* node = create_singly_linked_list_node(node_data);

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) {

fprintf(fptr, "%d", node->data);

node = node->next;
if (node) {

fprintf(fptr, "%s", sep);

void free_singly_linked_list(SinglyLinkedListNode* node) {

while (node) {

SinglyLinkedListNode* temp = node;

node = node->next;

free(temp);

// Complete the findMergeNode function below.

/*

* For your reference:

* SinglyLinkedListNode {

* int data;

* SinglyLinkedListNode* next;

* };
*

*/

int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {

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

You might also like