Created
October 5, 2022 18:10
-
-
Save Vaib215/0426991a3049d107a38275b3af871b32 to your computer and use it in GitHub Desktop.
Delete node at index in Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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