Skip to content

Instantly share code, notes, and snippets.

@Vaib215
Created October 5, 2022 18:03
Show Gist options
  • Save Vaib215/9038bb71f2e711165d30a43f22bfc627 to your computer and use it in GitHub Desktop.
Save Vaib215/9038bb71f2e711165d30a43f22bfc627 to your computer and use it in GitHub Desktop.
Insert new node at tail 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 insertAtTail(Node **head, int data){
Node *temp = *head;
Node *newNode = new Node(data);
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = newNode;
}
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);
insertAtTail(&head,7);
insertAtTail(&head,9);
printLL(&head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment