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
91 changes: 91 additions & 0 deletions Linked_List/Detect_cycle_in_LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node* next;

Node(int val)
{
data = val;
next = NULL;
}
};

void loopHere(Node* head, Node* tail, int position)
{
if(position==0) return;

Node* walk = head;
for(int i=1; i<position; i++)
walk = walk->next;
tail->next = walk;
}


class Solution
{
public:
//Function to check if the linked list has a loop.
bool detectLoop(Node* head)

{

Node *slow=head,*fast=head;

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

slow=slow->next;

fast=fast->next->next;



if(slow==fast)

return true;

}

return false;

}
};



int main()
{
int t;
cin>>t;
while(t--)
{
int n, num;
cin>>n;

Node *head, *tail;
cin>> num;
head = tail = new Node(num);

for(int i=0 ; i<n-1 ; i++)
{
cin>> num;
tail->next = new Node(num);
tail = tail->next;
}

int pos;
cin>> pos;
loopHere(head,tail,pos);

Solution ob;
if(ob.detectLoop(head) )
cout<< "True\n";
else
cout<< "False\n";
}
return 0;
}

84 changes: 84 additions & 0 deletions Linked_List/Flattening_a_LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;

// Linked list Node
struct Node {
int data;
struct Node* next;
struct Node* bottom;

Node(int x)
{
data = x;
next = NULL;
bottom = NULL;
}
};

// comparator function for priority queue
struct mycomp {
bool operator()(Node* a, Node* b)
{
return a->data > b->data;
}
};

void flatten(Node* root)
{
priority_queue<Node*, vector<Node*>, mycomp> p;
// pushing main link nodes into priority_queue.
while (root != NULL) {
p.push(root);
root = root->next;
}

// Extracting the minimum node
// while priority queue is not empty
while (!p.empty()) {

// extracting min
auto k = p.top();
p.pop();

// printing least element
cout << k->data << " ";
if (k->bottom)
p.push(k->bottom);
}
}

// Driver's code
int main(void)
{
// This code builds the flattened linked list
// of first picture in this article ;
Node* head = new Node(5);
auto temp = head;
auto bt = head;
bt->bottom = new Node(7);
bt->bottom->bottom = new Node(8);
bt->bottom->bottom->bottom = new Node(30);
temp->next = new Node(10);

temp = temp->next;
bt = temp;
bt->bottom = new Node(20);
temp->next = new Node(19);
temp = temp->next;
bt = temp;
bt->bottom = new Node(22);
bt->bottom->bottom = new Node(50);
temp->next = new Node(28);
temp = temp->next;
bt = temp;
bt->bottom = new Node(35);
bt->bottom->bottom = new Node(40);
bt->bottom->bottom->bottom = new Node(45);

// Function call
flatten(head);
cout << endl;
return 0;
}
// this code is contributed by user_990i
83 changes: 83 additions & 0 deletions Linked_List/Middle_of_LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <bits/stdc++.h>
using namespace std;

// Link list node
struct node
{
int data;
struct node* next;
};

// Function to get the middle of
// the linked list
void printMiddle(struct node* head)
{
int count = 0;
struct node* mid = head;

while (head != NULL)
{

// Update mid, when 'count'
// is odd number
if (count & 1)
mid = mid->next;

++count;
head = head->next;
}

// If empty list is provided
if (mid != NULL)
printf("The middle element is [%d]\n\n",
mid->data);
}

void push(struct node** head_ref, int new_data)
{

// Allocate node
struct node* new_node = (struct node*)malloc(
sizeof(struct node));

// Put in the data
new_node->data = new_data;

// Link the old list off the new node
new_node->next = (*head_ref);

// Move the head to point to
// the new node
(*head_ref) = new_node;
}

// A utility function to print
// a given linked list
void printList(struct node* ptr)
{
while (ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}

// Driver code
int main()
{

// Start with the empty list
struct node* head = NULL;
int i;

for(i = 5; i > 0; i--)
{
push(&head, i);
printList(head);
printMiddle(head);
}
return 0;
}

// This code is contributed by ac121102
89 changes: 89 additions & 0 deletions Linked_List/Nth node from end.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//{ Driver Code Starts
// C program to find n'th Node in linked list
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;

/* Link list Node */
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};


/* Function to get the nth node from the last of a linked list*/
int getNthFromLast(struct Node* head, int n);



/* Driver program to test above function*/
int main()
{
int T,i,n,l,k;

cin>>T;

while(T--){
struct Node *head = NULL, *tail = NULL;

cin>>n>>k;
int firstdata;
cin>>firstdata;
head = new Node(firstdata);
tail = head;
for(i=1;i<n;i++)
{
cin>>l;
tail->next = new Node(l);
tail = tail->next;
}

cout<<getNthFromLast(head, k)<<endl;
}
return 0;
}
// } Driver Code Ends


/* struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
*/

//Function to find the data of nth node from the end of a linked list.
int getNthFromLast(Node *head, int n)

{

int cnt=0;

Node* temp=head;

while(head){

head=head->next;

cnt++;

}

if(cnt<n) return -1;

cnt=cnt-n;

while(cnt--)temp=temp->next;

return temp->data;

}