class Solution{ public: //Function to insert a node at the beginning of the linked list. Node *insertAtBegining(Node *head, int x) { Node* newnode=new Node(x); newnode->next=head; return newnode; } //Function to insert a node at the end of the linked list. Node *insertAtEnd(Node *head, int x) { if(!head) { Node* newnode=new Node(x); return newnode; } Node* temp=head; while(temp->next!=NULL) { temp=temp->next; } Node* newnode=new Node(x); temp->next=newnode; return head; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter