Skip to content

Instantly share code, notes, and snippets.

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