0% found this document useful (0 votes)
12 views

All Codes

Uploaded by

2023uma0207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

All Codes

Uploaded by

2023uma0207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

CONTENTS

1 EXAMPLE OF CLASS CREATION/PUBLIC/ PRIVATE/CONSTRUCTOR/GETTER SETTER


2 AMBIGUITY

3 CONCEPT

4 HIERARCHIAL INHERITANCE

5 INHERITANCE

6 MULTI LEVEL INHERITANCE

7 MULTIPLE INHERITANCE

8 POLYMORPHISM

9 SINGLE INHERITANCE

10 CIRCULAR LINKED LIST

11 DOUBLY LINKED LIST

12 SINGLY LINKED LIST

13 LINKED LIST

14 REVERSE IN K GROUP

15 LOOP

16 DUPLICATES

17 MERGE LIST

18 SORT

19 PALINDROME

20 ADD

21 CLONE

22 MERGE SORT
EXAMPLE OF CLASS CREATION/PUBLIC/
PRIVATE/CONSTRUCTOR/GETTER SETTER

#include<iostream>

using namespace std;

class Hero {

//proper es

private:

int health;

public:

char *name;

char level;

sta c int meToComplete;

Hero() {

cout << "Simple constructor called" << endl;

name = new char[100];

//Paramerterised Constructor

Hero(int health) {

this -> health = health;

Hero(int health, char level) {

this -> level = level;

this -> health = health;

//copy constructor

Hero(Hero& temp) {
char *ch = new char[strlen(temp.name) + 1];

strcpy(ch, temp.name);

this->name = ch;

cout << "Copy constructor called" << endl;

this->health = temp.health;

this->level = temp.level;

void print(){

cout << endl;

cout << "[ Name: " << this->name<< " ,";

cout << "health: " << this->health << " ,";

cout <<"level: " << this->level << " ]";

cout << endl << endl;

int getHealth() {

return health;

char getLevel() {

return level;

void setHealth(int h) {

health = h;

void setLevel(char ch) {

level = ch;

void setName(char name[]) {

strcpy(this->name, name);
}

sta c int random() {

return meToComplete ;

//Destructor

~Hero() {

cout << "Destructor bhai called" << endl;

};

int Hero:: meToComplete = 5;

int main() {

//cout << Hero:: meToComplete << endl;

cout << Hero::random() << endl;

// Hero a;

// cout << a. meToComplete << endl;

// Hero b;

// b. meToComplete = 10 ;

// cout << a. meToComplete << endl;

// cout << b. meToComplete << endl;

// //Sta c

// Hero a;
// //Dynamic

// Hero *b = new Hero();

// //manually destructor call

// delete b;

// Hero hero1;

// hero1.setHealth(12);

// hero1.setLevel('D');

// char name[7] = "Babbar";

// hero1.setName(name);

// //hero1.print();

// //use default copy constructor

// Hero hero2(hero1);

// //hero2.print();

// // Hero hero2 = hero1;

// hero1.name[0] = 'G';

// hero1.print();

// hero2.print();

// hero1 = hero2;

// hero1.print();

// hero2.print();

// //Hero ;
// //object created sta cally

// Hero ramesh(10);

// //cout << "Address of ramesh " << &ramesh << endl;

// ramesh.print();

// //dynamically

// Hero *h = new Hero(11);

// h->print();

// Hero temp(22, 'B');

// temp.print();

/*

//sta c alloca on

Hero a;

a.setHealth(80);

a.setLevel('B');

cout << "level is " << a.level << endl;

cout << " health is " << a.getHealth() << endl;

//dynamicallly

Hero *b = new Hero;

b->setLevel('A');

b->setHealth(70);

cout << "level is " << (*b).level << endl;

cout << " health is " << (*b).getHealth() << endl;

cout << "level is " << b->level << endl;

cout << " health is " << b->getHealth() << endl;

*/

// //crea on of Object
// Hero ramesh;

// cout << "Size of Ramesh is " << sizeof(ramesh) << endl;

// cout << "Ramesh health is " << ramesh.getHealth() << endl;

// //use se er

// ramesh.setHealth(70);

// ramesh.level = 'A';

// cout << "health is: " << ramesh.getHealth() << endl;

// cout << "Level is: " << ramesh.level << endl;

// //cout << "size : " << sizeof(h1) << endl;

return 0;

AMBUIGUITY
#include<iostream>

using namespace std;

class A {

public:

void func() {

cout << " I am A" << endl;

};

class B {

public:

void func() {

cout << " I am B" << endl;

};

class C: public A, public B {

};
int main() {

C obj;

//obj.func();

obj.A::func() ;

obj.B::func();

return 0;

CONCEPT
#include<iostream>

using namespace std;

class Student {

private:

string name;

int age;

int height;

public:

int getAge() {

return this->age;

};

int main() {

Student first;

cout << "Sab sahi chalra hai" << endl;

return 0;}
HIERARCHIAL INHERITANCE
#include<iostream>

using namespace std;

//Hierarchical Inheritance

class A {

public:

void func1() {

cout << "Inside Funcion 1" << endl;

};

class B: public A {

public:

void func2() {

cout << "Inside Funcion 2" << endl;

};

class C: public A {

public:

void func3() {

cout << "Inside Funcion 3" << endl;

};

int main() {

A object1;

object1.func1();

B object2;

object2.func1();

object2.func2();
C object3;

object3.func1();

object3.func3();

return 0;

INHERITANCE
#include<iostream>

using namespace std;

class Human {

private:

int height;

public:

int weight;

private:

int age;

public:

int getAge() {

return this->age;

void setWeight(int w) {

this->weight = w;

};
class Male: private Human {

public:

string color;

void sleep() {

cout << "Male Sleeping" << endl;

int getHeight() {

return this->height;

};

int main() {

Male m1;

//cout << m1.height << endl;

/*

Male object1;

cout << object1.age << endl;

cout << object1.weight << endl;

cout << object1.height << endl;

cout << object1.color << endl;

object1.setWeight(84);

cout << object1.weight << endl;

object1.sleep();
*/

return 0;

MULTI LEVEL INHERITANCE


#include<iostream>

using namespace std;

class Animal {

public:

int age;

int weight;

public:

void speak() {

cout << "Speaking " << endl;

};

class Dog: public Animal {

};

class GermanShepherd: public Dog {

};

int main() {

GermanShepherd g;

g.speak();
return 0;

MULTIPLE INHERITANCE

#include<iostream>

using namespace std;

class Animal {

public:

int age;

int weight;

public:

void bark() {

cout << "Barking " << endl;

};

class Human {

public:

string color;

public:

void speak() {

cout << "Speaking " << endl;

};

//Mul ple Inheritance


class Hybrid: public Animal, public Human {

};

int main() {

Hybrid obj1;

obj1.speak();

obj1.bark();

return 0;

POLYMORPHISM

#include<iostream>

using namespace std;

class A {

public:

void sayHello() {

cout << "Hello Love Babbar" << endl;

int sayHello(char name) {

cout << "Hello Love Babbar" << endl;

return 1;

void sayHello(string name) {

cout << "Hello " << name << endl;

}
};

class B {

public:

int a;

int b;

public:

int add() {

return a+b;

void operator+ (B &obj) {

/* int value1 = this -> a;

int value2 = obj.a;

cout << "output " << value2 - value1 << endl;

*/

cout << "Hello Babbar" << endl;

void operator() () {

cout << "main Bracket hu " << this->a << endl;

};

class Animal {

public:

void speak() {

cout << "Speaking "<< endl;

};

class Dog: public Animal {


public:

void speak() {

cout << "Barking " << endl;

};

int main() {

Dog obj;

obj.speak();

/*

B obj1, obj2;

obj1.a = 4;

obj2.a = 7;

obj1 + obj2;

obj1();*/

/*

A obj;

obj.sayHello();

*/

return 0;

}
SINGLE INHERITANCE
#include<iostream>

using namespace std;

class Animal {

public:

int age;

int weight;

public:

void speak() {

cout << "Speaking " << endl;

};

class Dog: public Animal {

};

int main() {

Dog d;

d.speak();

cout << d.age << endl;

return 0;

}
CIRCULAR LINKED LIST
#include<iostream>

#include<map>

using namespace std;

class Node {

public:

int data;

Node* next;

//constrcutor

Node(int d) {

this->data = d;

this->next = NULL;

~Node() {

int value = this->data;

if(this->next != NULL) {

delete next;

next = NULL;

cout << " memory is free for node with data " << value << endl;

};

void insertNode(Node* &tail, int element, int d) {

//empty list

if(tail == NULL) {

Node* newNode = new Node(d);

tail = newNode;

newNode -> next = newNode;


}

else{

//non-empty list

//assuming that the element is present in the list

Node* curr = tail;

while(curr->data != element) {

curr = curr -> next;

//element found -> curr is represen ng element wala node

Node* temp = new Node(d);

temp -> next = curr -> next;

curr -> next = temp;

void print(Node* tail) {

Node* temp = tail;

//empty list

if(tail == NULL) {

cout << "List is Empty "<< endl;

return ;

do {

cout << tail -> data << " ";

tail = tail -> next;

} while(tail != temp);
cout << endl;

void deleteNode(Node* &tail, int value) {

//empty list

if(tail == NULL) {

cout << " List is empty, please check again" << endl;

return;

else{

//non-empty

//assuming that "value" is present in the Linked List

Node* prev = tail;

Node* curr = prev -> next;

while(curr -> data != value) {

prev = curr;

curr = curr -> next;

prev -> next = curr -> next;

//1 Node Linked List

if(curr == prev) {

tail = NULL;

//>=2 Node linked list

else if(tail == curr ) {

tail = prev;

curr -> next = NULL;


delete curr;

bool isCircularList(Node* head) {

//empty list

if(head == NULL) {

return true;

Node* temp = head -> next;

while(temp != NULL && temp != head ) {

temp = temp -> next;

if(temp == head ) {

return true;

return false;

bool detectLoop(Node* head) {

if(head == NULL)

return false;

map<Node*, bool> visited;

Node* temp = head;

while(temp !=NULL) {
//cycle is present

if(visited[temp] == true) {

return true;

visited[temp] = true;

temp = temp -> next;

return false;

int main() {

Node* tail = NULL;

// insertNode(tail, 5, 3);

//print(tail);

// insertNode(tail, 3, 5);

// print(tail);

/*

insertNode(tail, 5, 7);

print(tail);

insertNode(tail, 7, 9);

print(tail);

insertNode(tail, 5, 6);

print(tail);
insertNode(tail, 9, 10);

print(tail);

insertNode(tail, 3, 4);

print(tail);

deleteNode(tail, 5);

print(tail);

*/

if(isCircularList(tail)) {

cout << " Linked List is Circular in nature" << endl;

else{

cout << "Linked List is not Circular " << endl;

return 0;

DOUBLY LINKED LIST


#include<iostream>

using namespace std;

class Node {

public:

int data;

Node* prev;

Node* next;

//constructor

Node(int d ) {

this-> data = d;

this->prev = NULL;

this->next = NULL;
}

~Node() {

int val = this -> data;

if(next != NULL) {

delete next;

next = NULL;

cout << "memory free for node with data "<< val << endl;

};

//traversing a linked list

void print(Node* head) {

Node* temp = head ;

while(temp != NULL) {

cout << temp -> data << " ";

temp = temp -> next;

cout << endl;

//gives length of Linked List

int getLength(Node* head) {

int len = 0;

Node* temp = head ;

while(temp != NULL) {

len++;

temp = temp -> next;

return len;

}
void insertAtHead(Node* &tail, Node* &head, int d) {

//empty list

if(head == NULL) {

Node* temp = new Node(d);

head = temp;

tail = temp;

else{

Node* temp = new Node(d);

temp -> next = head;

head -> prev = temp;

head = temp;

void insertAtTail(Node* &tail,Node* &head, int d) {

if(tail == NULL) {

Node* temp = new Node(d);

tail = temp;

head = temp;

else{

Node* temp = new Node(d);

tail -> next = temp;

temp -> prev = tail;

tail = temp;

void insertAtPosi on(Node* & tail, Node* &head, int posi on, int d) {
//insert at Start

if(posi on == 1) {

insertAtHead(tail,head, d);

return;

Node* temp = head;

int cnt = 1;

while(cnt < posi on-1) {

temp = temp->next;

cnt++;

//inser ng at Last Posi on

if(temp -> next == NULL) {

insertAtTail(tail,head,d);

return ;

//crea ng a node for d

Node* nodeToInsert = new Node(d);

nodeToInsert ->next = temp -> next;

temp -> next -> prev = nodeToInsert;

temp -> next = nodeToInsert;

nodeToInsert -> prev = temp;

void deleteNode(int posi on, Node* & head) {

//dele ng first or start node

if(posi on == 1) {

Node* temp = head;

temp -> next -> prev = NULL;


head = temp ->next;

temp -> next = NULL;

delete temp;

else

//dele ng any middle node or last node

Node* curr = head;

Node* prev = NULL;

int cnt = 1;

while(cnt < posi on) {

prev = curr;

curr = curr -> next;

cnt++;

curr -> prev = NULL;

prev -> next = curr -> next;

curr -> next = NULL;

delete curr;

int main() {

Node* head = NULL;

Node* tail = NULL;

print(head);
//cout << getLength(head) << endl;

insertAtHead(tail,head, 11);

print(head);

cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

insertAtHead(tail,head, 13);

print(head);

cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

insertAtHead(tail,head, 8);

print(head);

cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

insertAtTail(tail,head, 25);

print(head);

cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

insertAtPosi on(tail, head, 2, 100);

print(head);

cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

insertAtPosi on(tail, head, 1, 101);

print(head);

cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

insertAtPosi on(tail, head, 7, 102);

print(head);
cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

deleteNode(7, head);

print(head);

cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

return 0;

SINGLY LINKED LIST


#include<iostream>

#include<map>

using namespace std;

class Node {

public:

int data;

Node* next;

//constructor

Node(int data) {

this -> data = data;

this -> next = NULL;

//destructor

~Node() {

int value = this -> data;

//memory free
if(this->next != NULL) {

delete next;

this->next = NULL;

cout << " memory is free for node with data " << value << endl;

};

void insertAtHead(Node* &head, int d) {

// new node create

Node* temp = new Node(d);

temp -> next = head;

head = temp;

void insertAtTail(Node* &tail, int d) {

// new node create

Node* temp = new Node(d);

tail -> next = temp;

tail = temp;

void print(Node* &head) {

if(head == NULL) {

cout << "List is empty "<< endl;

return ;

Node* temp = head;

while(temp != NULL ) {

cout << temp -> data << " ";

temp = temp -> next;


}

cout << endl;

void insertAtPosi on(Node* &tail, Node* & head, int posi on, int d) {

//insert at Start

if(posi on == 1) {

insertAtHead(head, d);

return;

Node* temp = head;

int cnt = 1;

while(cnt < posi on-1) {

temp = temp->next;

cnt++;

//inser ng at Last Posi on

if(temp -> next == NULL) {

insertAtTail(tail,d);

return ;

//crea ng a node for d

Node* nodeToInsert = new Node(d);

nodeToInsert -> next = temp -> next;

temp -> next = nodeToInsert;

}
void deleteNode(int posi on, Node* & head) {

//dele ng first or start node

if(posi on == 1) {

Node* temp = head;

head = head -> next;

//memory free start ndoe

temp -> next = NULL;

delete temp;

else

//dele ng any middle node or last node

Node* curr = head;

Node* prev = NULL;

int cnt = 1;

while(cnt < posi on) {

prev = curr;

curr = curr -> next;

cnt++;

prev -> next = curr -> next;

curr -> next = NULL;

delete curr;

bool isCircularList(Node* head) {

//empty list

if(head == NULL) {

return true;

}
Node* temp = head -> next;

while(temp != NULL && temp != head ) {

temp = temp -> next;

if(temp == head ) {

return true;

return false;

bool detectLoop(Node* head) {

if(head == NULL)

return false;

map<Node*, bool> visited;

Node* temp = head;

while(temp !=NULL) {

//cycle is present

if(visited[temp] == true) {

cout << "Present on element " << temp->data << endl;

return true;

visited[temp] = true;

temp = temp -> next;

}
return false;

Node* floydDetectLoop(Node* head) {

if(head == NULL)

return NULL;

Node* slow = head;

Node* fast = head;

while(slow != NULL && fast !=NULL) {

fast = fast -> next;

if(fast != NULL) {

fast = fast -> next;

slow = slow -> next;

if(slow == fast) {

cout << "present at " << slow -> data << endl;

return slow;

return NULL;

Node* getStar ngNode(Node* head) {

if(head == NULL)

return NULL;
Node* intersec on = floydDetectLoop(head);

Node* slow = head;

while(slow != intersec on) {

slow = slow -> next;

intersec on = intersec on -> next;

return slow;

void removeLoop(Node* head) {

if( head == NULL)

return;

Node* startOfLoop = getStar ngNode(head);

Node* temp = startOfLoop;

while(temp -> next != startOfLoop) {

temp = temp -> next;

temp -> next = NULL;

int main() {

//created a new node

Node* node1 = new Node(10);

//cout << node1 -> data << endl;


// cout << node1 -> next << endl;

//head pointed to node1

Node* head = node1;

Node* tail = node1;

//print(head);

insertAtTail(tail, 12);

//print(head);

insertAtTail(tail, 15);

//print(head);

insertAtPosi on(tail,head, 4, 22);

//print(head);

//cout << "head " << head -> data << endl;

//cout << "tail " << tail -> data << endl;

//deleteNode(4, head);

tail -> next = head ->next;

cout << "head " << head -> data << endl;

cout << "tail " << tail -> data << endl;

//print(head);

if(floydDetectLoop(head) != NULL) {

cout << "Cycle is present " << endl;

else

cout << "no cycle" << endl;


}

Node* loop = getStar ngNode(head);

cout << "loop starts at " << loop -> data << endl;

removeLoop(head);

print(head);

/*

print(head);

if(isCircularList(head)) {

cout << " Linked List is Circular in nature" << endl;

else{

cout << "Linked List is not Circular " << endl;

*/

return 0;

LINKED LIST
#include<iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

//constructor
Node(int data) {

this -> data = data;

this -> next = NULL;

//destructor

~Node() {

int value = this -> data;

//memory free

if(this->next != NULL) {

delete next;

this->next = NULL;

cout << " memory is free for node with data " << value << endl;

};

void insertAtHead(Node* &head, int d) {

// new node create

Node* temp = new Node(d);

temp -> next = head;

head = temp;

void insertAtTail(Node* &tail, int d) {

// new node create

Node* temp = new Node(d);

tail -> next = temp;

tail = temp;

void print(Node* &head) {

Node* temp = head;


while(temp != NULL ) {

cout << temp -> data << " ";

temp = temp -> next;

cout << endl;

void insertAtPosi on(Node* &tail, Node* & head, int posi on, int d) {

//insert at Start

if(posi on == 1) {

insertAtHead(head, d);

return;

Node* temp = head;

int cnt = 1;

while(cnt < posi on-1) {

temp = temp->next;

cnt++;

//inser ng at Last Posi on

if(temp -> next == NULL) {

insertAtTail(tail,d);

return ;

//crea ng a node for d

Node* nodeToInsert = new Node(d);

nodeToInsert -> next = temp -> next;


temp -> next = nodeToInsert;

void deleteNode(int posi on, Node* & head) {

//dele ng first or start node

if(posi on == 1) {

Node* temp = head;

head = head -> next;

//memory free start ndoe

temp -> next = NULL;

delete temp;

else

//dele ng any middle node or last node

Node* curr = head;

Node* prev = NULL;

int cnt = 1;

while(cnt < posi on) {

prev = curr;

curr = curr -> next;

cnt++;

prev -> next = curr -> next;

curr -> next = NULL;

delete curr;

int main() {
return 0;

REVERSE IN K GROUP
/****************************************************************

Following is the Linked List node structure

class Node

public:

int data;

Node *next;

Node(int data)

this->data = data;

this->next = NULL;

};

*****************************************************************/

Node* kReverse(Node* head, int k) {

//base call

if(head == NULL) {

return NULL;

}
//step1: reverse first k nodes

Node* next = NULL;

Node* curr = head;

Node* prev = NULL;

int count= 0;

while( curr != NULL && count < k ) {

next = curr -> next;

curr -> next = prev;

prev = curr;

curr = next;

count++;

//step2: Recursion dekhlega aage ka

if(next != NULL) {

head -> next = kReverse(next,k);

//step3: return head of reversed list

return prev;

LOOP
/*************************************************

class Node {

public :

int data;
Node *next;

Node(int data) {

this -> data = data;

this -> next = NULL;

};

*************************************************/

Node* floydDetectLoop(Node* head) {

if(head == NULL)

return NULL;

Node* slow = head;

Node* fast = head;

while(slow != NULL && fast !=NULL) {

fast = fast -> next;

if(fast != NULL) {

fast = fast -> next;

slow = slow -> next;

if(slow == fast) {

return slow;

}
return NULL;

Node* getStar ngNode(Node* head) {

if(head == NULL)

return NULL;

Node* intersec on = floydDetectLoop(head);

if(intersec on == NULL)

return NULL;

Node* slow = head;

while(slow != intersec on) {

slow = slow -> next;

intersec on = intersec on -> next;

return slow;

Node *removeLoop(Node *head)

if( head == NULL)

return NULL;
Node* startOfLoop = getStar ngNode(head);

if(startOfLoop == NULL)

return head;

Node* temp = startOfLoop;

while(temp -> next != startOfLoop) {

temp = temp -> next;

temp -> next = NULL;

return head;

DUPLICATES
/************************************************************

Following is the linked list node structure.

class Node

public:

int data;

Node* next;

Node(int data)

this->data = data;

this->next = NULL;

};
************************************************************/

Node * uniqueSortedList(Node * head) {

//empty List

if(head == NULL)

return NULL;

//non empty list

Node* curr = head;

while(curr != NULL) {

if( (curr -> next != NULL) && curr -> data == curr -> next -> data) {

Node* next_next = curr ->next -> next;

Node* nodeToDelete = curr -> next;

delete(nodeToDelete);

curr -> next = next_next;

else //not equal

curr = curr -> next;

return head;

MERGELIST
/************************************************************

Following is the linked list node structure.


template <typename T>

class Node {

public:

T data;

Node* next;

Node(T data) {

next = NULL;

this->data = data;

~Node() {

if (next != NULL) {

delete next;

};

************************************************************/

void solve(Node<int>* first, Node<int>* second) {

Node* curr1 = first;

Node* next1 = curr1 -> next;

Node* curr2 = second;

Node* next2 = curr2 -> next;

while(next1 != NULL && curr2 != NULL) {


if( (curr2 -> data >= curr1 -> data )

&& ( curr2 -> data <= next1 -> data)) {

curr1 -> next = curr2;

curr2 -> next = next1;

curr1 = curr2;

curr2 = next2;

else {

Node<int>* sortTwoLists(Node<int>* first, Node<int>* second)

if(first == NULL)

return second;

if(second == NULL)

return first;

if(first -> data <= second -> data ){

solve(first, second);

else
{

solve(second, first);

SORT
/********************************

class Node

public:

int data;

Node *next;

Node(int data)

this->data = data;

this->next = NULL;

};

********************************/

void insertAtTail(Node* &tail, Node* curr ) {

tail -> next = curr;

tail = curr;

Node* sortList(Node *head)

Node* zeroHead = new Node(-1);


Node* zeroTail = zeroHead;

Node* oneHead = new Node(-1);

Node* oneTail = oneHead;

Node* twoHead = new Node(-1);

Node* twoTail = twoHead;

Node* curr = head;

// create separate list 0s, 1s and 2s

while(curr != NULL) {

int value = curr -> data;

if(value == 0) {

insertAtTail(zeroTail, curr);

else if(value == 1) {

insertAtTail(oneTail, curr);

else if(value == 2) {

insertAtTail(twoTail, curr);

curr = curr -> next;

//merge 3 sublist

// 1s list not empty

if(oneHead -> next != NULL) {

zeroTail -> next = oneHead -> next;

}
else {

//1s list -> empty

zeroTail -> next = twoHead -> next;

oneTail -> next = twoHead -> next;

twoTail -> next = NULL;

//setup head

head = zeroHead -> next;

//delete dummy nodes

delete zeroHead;

delete oneHead;

delete twoHead;

return head;

PALINDROME
class Solu on{

private:

Node* getMid(Node* head ) {

Node* slow = head;

Node* fast = head -> next;

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

fast = fast -> next -> next;

slow = slow -> next;

}
return slow;

Node* reverse(Node* head) {

Node* curr = head;

Node* prev = NULL;

Node* next = NULL;

while(curr != NULL) {

next = curr -> next;

curr -> next = prev;

prev = curr;

curr = next;

return prev;

public:

//Func on to check whether the list is palindrome.

bool isPalindrome(Node *head)

if(head -> next == NULL) {

return true;

//step 1 -> find Middle

Node* middle = getMid(head);

//cout << "Middle " << middle->data << endl;

//step2 -> reverse List a er Middle

Node* temp = middle -> next;

middle -> next = reverse(temp);


//step3 - Compare both halves

Node* head1 = head;

Node* head2 = middle -> next;

while(head2 != NULL) {

if(head2->data != head1->data) {

return 0;

head1 = head1 -> next;

head2 = head2 -> next;

//step4 - repeat step 2

temp = middle -> next;

middle -> next = reverse(temp);

return true;

};

ADD
class Solu on

private:

Node* reverse(Node* head) {

Node* curr = head;

Node* prev = NULL;

Node* next = NULL;


while(curr != NULL) {

next = curr -> next;

curr -> next = prev;

prev = curr;

curr = next;

return prev;

void insertAtTail(struct Node* &head, struct Node* &tail, int val) {

Node* temp = new Node(val);

//empty list

if(head == NULL) {

head = temp;

tail = temp;

return;

else

tail -> next = temp;

tail = temp;

struct Node* add(struct Node* first, struct Node* second) {

int carry = 0;

Node* ansHead = NULL;

Node* ansTail = NULL;


while(first != NULL || second != NULL || carry != 0) {

int val1 = 0;

if(first != NULL)

val1 = first -> data;

int val2 = 0;

if(second !=NULL)

val2 = second -> data;

int sum = carry + val1 + val2;

int digit = sum%10;

//create node and add in answer Linked List

insertAtTail(ansHead, ansTail, digit);

carry = sum/10;

if(first != NULL)

first = first -> next;

if(second != NULL)

second = second -> next;

return ansHead;

public:

//Func on to add two numbers represented by linked list.

struct Node* addTwoLists(struct Node* first, struct Node* second)

{
//step 1 - reverse input LL

first = reverse(first);

second = reverse(second);

//step2 - add 2 LL

Node* ans = add(first, second);

//step 3

ans = reverse(ans);

return ans;

};

CLONE
class Solu on

private:

void insertAtTail(Node* &head, Node* &tail, int d) {

Node* newNode = new Node(d);

if(head == NULL) {

head = newNode;

tail = newNode;

else

tail -> next = newNode;

tail = newNode;

void print(Node* head) {


while(head != NULL) {

cout << head -> data << " ";

head = head -> next;

}cout << endl;

void printRandom(Node* head) {

while(head != NULL) {

cout << " head data: " << head->data <<" ";

if(head ->arb != NULL) {

cout << " head random data" << head -> arb ->data;

else

cout << " head random data: NULL";

head = head -> next;

cout << endl;

public:

Node *copyList(Node *head)

//step 1: Create a Clone List

Node* cloneHead = NULL;

Node* cloneTail = NULL;

Node* temp = head;

while(temp != NULL) {

insertAtTail(cloneHead, cloneTail, temp->data);


temp = temp -> next;

// step 2: insert nodes of Clone List in between originalList

Node* originalNode = head;

Node* cloneNode = cloneHead;

while(originalNode != NULL && cloneNode != NULL) {

Node* next = originalNode -> next;

originalNode -> next = cloneNode;

originalNode = next;

next = cloneNode -> next;

cloneNode -> next = originalNode;

cloneNode = next;

// step 3: Random pointer copy

originalNode = head;

cloneNode = cloneHead;

while(originalNode != NULL && cloneNode != NULL) {

if(originalNode -> arb != NULL) {

cloneNode -> arb = originalNode -> arb -> next;

else

cloneNode -> arb = NULL;

}
cloneNode = cloneNode -> next;

originalNode = originalNode -> next;

//step 4: revert step 2 changes

Node* original = head;

Node* copy = cloneHead;

while (original && copy)

original->next =

original->next? original->next->next : original->next;

copy->next = copy->next?copy->next->next:copy->next;

original = original->next;

copy = copy->next;

// step 5 answer return

return cloneHead;

};
MERGESORT
class Solu on

private:

void insertAtTail(Node* &head, Node* &tail, int d) {

Node* newNode = new Node(d);

if(head == NULL) {

head = newNode;

tail = newNode;

else

tail -> next = newNode;

tail = newNode;

void print(Node* head) {

while(head != NULL) {

cout << head -> data << " ";

head = head -> next;

}cout << endl;

void printRandom(Node* head) {

while(head != NULL) {

cout << " head data: " << head->data <<" ";

if(head ->arb != NULL) {

cout << " head random data" << head -> arb ->data;
}

else

cout << " head random data: NULL";

head = head -> next;

cout << endl;

public:

Node *copyList(Node *head)

//step 1: Create a Clone List

Node* cloneHead = NULL;

Node* cloneTail = NULL;

Node* temp = head;

while(temp != NULL) {

insertAtTail(cloneHead, cloneTail, temp->data);

temp = temp -> next;

// step 2: insert nodes of Clone List in between originalList

Node* originalNode = head;

Node* cloneNode = cloneHead;

while(originalNode != NULL && cloneNode != NULL) {


Node* next = originalNode-> next;

originalNode -> next = cloneNode;

originalNode = next;

next = cloneNode -> next;

cloneNode -> next = originalNode;

cloneNode = next;

// step 3: Random pointer copy

originalNode = head;

cloneNode = cloneHead;

while(originalNode != NULL && cloneNode != NULL) {

if(originalNode -> arb != NULL) {

cloneNode -> arb = originalNode -> arb -> next;

else

cloneNode -> arb = NULL;

cloneNode = cloneNode -> next;

originalNode = originalNode -> next;

//step 4: revert step 2 changes

Node* original = head;


Node* copy = cloneHead;

while (original && copy)

original->next =

original->next? original->next->next : original->next;

copy->next = copy->next?copy->next->next:copy->next;

original = original->next;

copy = copy->next;

// step 5 answer return

return cloneHead;

};

SIR NOTES

You might also like