Skip to content

Instantly share code, notes, and snippets.

@Vaib215
Last active October 5, 2022 18:11
Show Gist options
  • Save Vaib215/9312f026c4962146b67ae087d2bdff57 to your computer and use it in GitHub Desktop.
Save Vaib215/9312f026c4962146b67ae087d2bdff57 to your computer and use it in GitHub Desktop.
Search at a specific index of Linked List
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data){
this->data = data;
this->next = NULL;
}
};
void searchInLL(Node **head,int key){
Node *temp = *head;
int index=0;
while(temp!=NULL){
if(temp->data==key){
cout<<key<<" found at index: "<<index<<endl;
return;
}
index++;
temp = temp->next;
}
cout<<key<<" not found in LL"<<endl;
}
void printLL(Node **head){
Node *temp = *head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
int main(){
Node* head = new Node(5);
searchInLL(&head,7);
printLL(&head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment