Skip to content

Instantly share code, notes, and snippets.

@Vaib215
Created October 5, 2022 18:10
Show Gist options
  • Save Vaib215/0426991a3049d107a38275b3af871b32 to your computer and use it in GitHub Desktop.
Save Vaib215/0426991a3049d107a38275b3af871b32 to your computer and use it in GitHub Desktop.
Delete node at index in 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 deleteInLL(Node **head, int index){
Node *temp = *head;
int currInd = 0;
if(index<0) {
cout<<"Invalid index";
return;
}
while(temp!=NULL && currInd<index-1){
temp = temp->next;
currInd++;
}
if(temp==NULL) {
cout<<"Invalid index"<<endl;
return;
}
temp->next = temp->next->next;
}
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);
deleteInLL(&head,5);
printLL(&head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment