Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions C++/Detect-Loop-in-Linked-List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*Detect Loop in linked list
Given a linked list of N nodes. The task is to check if the linked list has a loop. Linked list can contain self loop.

Example 1:

Input:
N = 3
value[] = {1,3,4}
x = 2
Output: True
Explanation: In above test case N = 3.
The linked list with nodes N = 3 is
given. Then value of x=2 is given which
means last node is connected with xth
node of linked list. Therefore, there
exists a loop.
Example 2:

Input:
N = 4
value[] = {1,8,3,4}
x = 0
Output: False
Explanation: For N = 4 ,x = 0 means
then lastNode->next = NULL, then
the Linked list does not contains
any loop.*/

#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
};

void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

int detectLoop(Node* list)
{
Node *slow_p = list, *fast_p = list;

while (slow_p && fast_p && fast_p->next) {
slow_p = slow_p->next;
fast_p = fast_p->next->next;
if (slow_p == fast_p) {
return 1;
}
}
return 0;
}

int main()
{
Node* head = NULL;

push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 10);
head->next->next->next->next = head;
if (detectLoop(head))
cout << "Loop found";
else
cout << "No Loop";
return 0;
}
144 changes: 144 additions & 0 deletions C++/Flatten-Linked-List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
Flattening a Linked List
Medium Accuracy: 33.91% Submissions: 67762 Points: 4
Given a Linked List of size N, where every node represents a sub-linked-list and contains two pointers:
(i) a next pointer to the next node,
(ii) a bottom pointer to a linked list where this node is head.
Each of the sub-linked-list is in sorted order.
Flatten the Link List such that all the nodes appear in a single level while maintaining the sorted order.
Note: The flattened list will be printed using the bottom pointer instead of next pointer.

Example 1:
Input:
5 -> 10 -> 19 -> 28
| | | |
7 20 22 35
| | |
8 50 40
| |
30 45
Output: 5-> 7-> 8- > 10 -> 19-> 20->
22-> 28-> 30-> 35-> 40-> 45-> 50.
Explanation:
The resultant linked lists has every
node in a single level.
(Note: | represents the bottom pointer.)

Example 2:
Input:
5 -> 10 -> 19 -> 28
| |
7 22
| |
8 50
|
30
Output: 5->7->8->10->19->20->22->30->50
Explanation:
The resultant linked lists has every
node in a single level.
(Note: | represents the bottom pointer.)
*/
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *right, *down;
};

Node* head ;

Node* merge(Node* a, Node* b)
{
if (a == NULL)
return b;
if (b == NULL)
return a;
Node* result;

if (a->data < b->data)
{
result = a;
result->down = merge(a->down, b);
}
else
{
result = b;
result->down = merge(a, b->down);
}
result->right = NULL;
return result;
}

Node* flatten(Node* root)
{
if (root == NULL || root->right == NULL)
return root;
return merge(root, flatten(root->right));
}

Node* push(Node* head_ref, int data)
{

Node* new_node = new Node();

new_node->data = data;
new_node->right = NULL;
new_node->down = head_ref;
head_ref = new_node;

return head_ref;
}

void printList()
{
Node* temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->down;
}
cout << endl;
}

// Driver code
int main()
{

/* Let us create the following linked list
5 -> 10 -> 19 -> 28
| | | |
V V V V
7 20 22 35
| | |
V V V
8 50 40
| |
V V
30 45
*/
head = push(head, 30);
head = push(head, 8);
head = push(head, 7);
head = push(head, 5);

head->right = push(head->right, 20);
head->right = push(head->right, 10);

head->right->right = push(head->right->right, 50);
head->right->right = push(head->right->right, 22);
head->right->right = push(head->right->right, 19);

head->right->right->right = push(head->right->right->right, 45);
head->right->right->right = push(head->right->right->right, 40);
head->right->right->right = push(head->right->right->right, 35);
head->right->right->right = push(head->right->right->right, 20);

// Flatten the list
head = flatten(head);

printList();
return 0;
}
94 changes: 94 additions & 0 deletions C++/Linked-list-is-pallindrome-or-not.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*Check if Linked List is Palindrome
Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.

Example 1:

Input:
N = 3
value[] = {1,2,1}
Output: 1
Explanation: The given linked list is
1 2 1 , which is a palindrome and
Hence, the output is 1.
Example 2:

Input:
N = 4
value[] = {1,2,3,4}
Output: 0
Explanation: The given linked list
is 1 2 3 4 , which is not a palindrome
and Hence, the output is 0.*/
#include<iostream>
#include<stack>
using namespace std;

class Node {
public:
int data;
Node* next;
};

void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

int countSum(Node* slow){
int res=1;
Node* temp = slow;
while(temp->next!=slow){
res++;
temp = temp->next;
}
return res;
}

bool isPalindrome(Node *head)
{
Node* curr= head;
int count=0, pos;
stack<int> v;
while(curr!=NULL){
count++;
v.push(curr->data);
curr = curr->next;
}
curr = head;
while(head!=NULL){
// cout<<"head: "<<head->data<<" top: "<<v.top()<<"\n";
if(head->data != v.top())
return 0;
head = head->next;
v.pop();
}

return 1;
}
void printList(struct Node* node)
{
// Print the list after loop removal
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}

int main()
{
Node* head = NULL;

push(&head, 1);
push(&head, 2);
push(&head, 1);
// printList(head);
if(isPalindrome(head)){
cout<<"It is a pallindrome";
}else{
cout<<"It is not a pallindrome";
}

}
Loading