Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

length_recursively #263

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
77 changes: 77 additions & 0 deletions DSA/Linked_list/Single Linked List/length_recurssivly.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

// input: 1 2 3 4 5 6 -1

// output:

// 1 2 3 4 5 6 -1
// 1 2 3 4 5 6
// the length of the linked list is:6

// code begin :

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

class node{
public:
int data ;
node*next ;
node (int data ){
this->data = data ;
next = NULL ;
}
};

// used to take the input of the node

node* takeinput(){
int data ;
cin>>data;
node*head = NULL;
while(data!=-1){
node*newnode = new node(data);
if(head==NULL){
head = newnode;
}
else {
node*temp = head;
while(temp->next!=NULL){
temp = temp->next;

}
temp->next =newnode;



}
cin>>data;
}
return head;
}

// used to print the full linked list
void print(node*head){
while(head!=NULL){
cout<<head->data<<" ";
head = head->next;

}
}

int length(node*head ,int l){
if(head==NULL) return l; // base case
// int l;

return length(head->next,l+=1); // recurrsive call
}

int main(){
// freopen("in.txt", "r", stdin);
// freopen("outp1.txt", "w", stdout);

node*head = takeinput();
print(head);
cout<<endl;
cout<<"the length of the linked list is:";
cout<<length(head,0);
}
107 changes: 107 additions & 0 deletions DSA/Linked_list/Single Linked List/merge_2_sorted_ll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// WAP TO MERGE TWO SORTED LL


// input : 1 2 3 4 -1
// 6 7 8 9 -1


// output :
// 1234
// 6789
// 12346789


// begin of the code :
#include <bits/stdc++.h>
using namespace std;

class node{
public:
int data;
node*next;
node(int data){
this->data = data;
next = NULL;
}
};

node*takeinput(){
int cnt = 0;
int data;
cin>>data;
node*head = NULL;
node*tail = NULL;
while(data!=-1){
// cnt is used to chek the lenght of the linked list...
cnt++;
node*newnode = new node(data);
if(head == NULL){
tail = newnode;
head = newnode;
}
else{
tail->next = newnode;
tail = newnode;
}
cin>>data;

}
if(cnt<1){
head = NULL;
return head;
}
return head;
}

// merging of the two linked list code is started

node*merging(node*h1 , node*h2){
node*tf = NULL; // tf is for first linked list tail pointing to the last node
node*hf = NULL; // hf is for first linked list tail pointing to the first node
if((h1->data) <(h2->data)){
// h1 = h1->next;
tf = h1;hf = h1;
h1 = h1->next;
}
else {

tf = h2;hf = h2; // h2 is for the second l l pointing to the first node
h2 = h2->next; // t2 stands for the last node in the linked list
}
while(h1!=NULL && h2!=NULL){
if(h1->data <= h2->data){
tf->next = h1;
tf = h1;
h1 = h1->next;
}
else {
tf->next = h2;
tf = h2;
h2 = h2->next;

}
}
if(h1!=NULL){
tf->next =h1;
}
else{
tf ->next = h2;
}
return hf;
}

// used to print the whole l l

void print(node*head){
while(head!=NULL){
cout<<head->data ;
head = head->next;
}
}
int main(){
node*h1 = takeinput();
node*h2 = takeinput();
print(h1);cout<<endl;print(h2);cout<<endl;
node*hf = merging(h1,h2);
print(hf);
}