Linked List Insertion

PHOTO EMBED

Thu Dec 07 2023 18:08:42 GMT+0000 (Coordinated Universal Time)

Saved by @nistha_jnn #c++

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;
    }
};
content_copyCOPY

https://practice.geeksforgeeks.org/problems/linked-list-insertion-1587115620/1?page=1&category=Linked%20List&sortBy=submissions