Linked List Insertion | Practice | GeeksforGeeks

Create a link list of size N according to the given input literals. Each integer input is accompanied by an indicator which can either be 0 or 1. If it is 0, insert the integer in the beginning of the link list. If it is 1, insert the integer at the end of the link list. 
Hint: When inserting at the end, make sure that you handle NULL explicitly. 
Example 1:
Input:
LinkedList: 9->0->5->1->6->1->2->0->5->0
Output: 5 2 9 5 6
Explanation:
Length of Link List = N = 5
9 0 indicated that 9 should be
inserted in the beginning. Modified
Link List = 9.
5 1 indicated that 5 should be
inserted in the end. Modified Link
List = 9,5.
6 1 indicated that 6 should be
inserted in the end. Modified Link
List = 9,5,6.
2 0 indicated that 2 should be
inserted in the beginning. Modified
Link List = 2,9,5,6.
5 0 indicated that 5 should be
inserted in the beginning. Modified
Link List = 5,2,9,5,6. 
Final linked list = 5, 2, 9, 5, 6.


Example 2:
Input:
LinkedList: 5->1->6->1->9->1
Output: 5 6 9


Your Task:
You only need to complete the functions insertAtBeginning() and insertAtEnd() that takes the head of link list and integer value of the data to be inserted as inputs and returns the head of the modified link list. 
Expected Time Complexity: O(1) for insertAtBeginning() and O(N) for insertAtEnd().
Expected Auxiliary Space: O(1) for both.
Constraints:
1 <= N <= 104
0
lakmtj5 days ago
class Solution{   //Function to insert a node at the beginning of the linked list.   Node insertAtBeginning(Node head, int x)   {       // code here       if(head==null){           head = new Node(x);           return head;       }       Node x1 = new Node(x);       x1.next=head;       head=x1;       //Node a = head;              //while(a!=null){       //if(a.data==0){           //x1.next=head;           //head=x1;         //  a=a.next;       //}       //}       return head;   }      //Function to insert a node at the end of the linked list.   Node insertAtEnd(Node head, int x)   {       // code here       if(head==null){           head = new Node(x);           return head;       }       Node a1 = head;       while(a1.next!=null){                  a1=a1.next;       }       Node x2 = new Node(x);       a1.next=x2;       return head;   }}
0
ghostlearner215 days ago
Java Solution Here !
Node insertAtBeginning(Node head, int x)   {       Node n = new Node(x);       n.next = head;       head = n;       return head;   }      //Function to insert a node at the end of the linked list.   Node insertAtEnd(Node head, int x)   {       Node newNode= new Node(x);       if(head==null){           return newNode;       }       Node n = head;       while(n.next!=null){           n = n.next;       }              n.next=newNode;       return head;   }
0
rohankundu8592 weeks ago
//C++ Solution 
 
 
//Function to insert a node at the beginning of the linked list.
Node *insertAtBegining(Node *head, int x)    {      // Your code here      if(head==NULL)      {          Node* p=new Node(x);          return p;      }      Node* ptr=new Node(x);      ptr->next=head;      head=ptr;      return head;   }
 
 
 
   //Function to insert a node at the end of the linked list.   Node *insertAtEnd(Node *head, int x)     {      // Your code here      if(head==NULL)      {          Node* p=new Node(x);          return p;      }      Node* temp=head;      while(temp->next!=NULL)      {          temp=temp->next;      }      Node* pr=new Node(x);      temp->next=pr;      return head;   }
0
pranavchandore20002 weeks ago
// { Driver Code Starts#include <bits/stdc++.h> using namespace std;
struct Node{   int data;   struct Node* next;      Node(int x){       data = x;       next = NULL;   }};void printList(Node* node) {    while (node != NULL) {        cout << node->data <<" ";        node = node->next;  }   cout<<"\n";}
 
// } Driver Code Ends/*Structure of the linked list node is asstruct Node { int data; struct Node * next; Node(int x) {   data = x;   next = NULL; }}; */
class Solution{ public:   //Function to insert a node at the beginning of the linked list.   Node *insertAtBegining(Node *head, int x) {             // Your code here      Node* n = new Node(x);                if(head == NULL){          return n;      }       n->next =head;     head =n;      return n;             }         //Function to insert a node at the end of the linked list.   Node *insertAtEnd(Node *head, int x)  {      // Your code here      Node* n = new Node(x);                 if(head == NULL){          return n;      }     Node* temp=head;            while(temp->next != NULL){          temp = temp->next;      }      temp->next =n;            return head;   }};
// { Driver Code Starts.int main() {    int t;   cin>>t;   while(t--)   {       int n;       cin>>n;       struct Node *head = NULL;       for (int i = 0; i < n; ++i)       {           int data, indicator;           cin>>data;           cin>>indicator;           Solution obj;           if(indicator)               head = obj.insertAtEnd(head, data);            else               head = obj.insertAtBegining(head, data);       }       printList(head);    }   return 0; }
 // } Driver Code Ends 
0
hharshit81183 weeks ago
simple implementation to fill the insert at begin and end function - - - 
struct Node *insertAtBegining(struct Node *head, int x) {   struct Node* temp = (struct Node*)malloc(sizeof(struct Node));   temp->data = x;   temp->next = NULL;   if(head == NULL){       return temp;   }   temp->next = head;   return temp;}struct Node *insertAtEnd(struct Node *head, int x)  {   struct Node* temp = (struct Node*)malloc(sizeof(struct Node));   temp->data = x;   temp->next = NULL;   if(head == NULL){       return temp;   }   struct Node* ptr = head;   while(ptr->next != NULL){       ptr = ptr->next;   }   ptr->next = temp;   return head;}
0
kushraval713 weeks ago
Guys, what's wrong in this C code: Its showing TLE: 
struct Node *insertAtBegining(struct Node *head, int x) {
struct Node *ptr = (struct Node*)malloc(sizeof(struct Node));ptr->data=x;ptr->next=head;head=ptr;return head;}
//Function to insert a node at the end of the linked list.struct Node *insertAtEnd(struct Node *head, int x)  {   struct Node *p = (struct Node*)malloc(sizeof(struct Node));   p->data=x;   struct Node *ptr=head;   while(ptr->next!=NULL) {       ptr=ptr->next;   }   ptr->next=p;   p->next=NULL;   return head;}
0
mohitraj31251 month ago
class Solution{ public:   //Function to insert a node at the beginning of the linked list.   Node *insertAtBegining(Node *head, int x) {      // Your code here      Node *temp=new Node(x);      temp->next = head;      return temp;   }      //Function to insert a node at the end of the linked list.   Node *insertAtEnd(Node *head, int x)  {      // Your code here      Node *temp = new Node(x);      if(head==NULL)      return temp;            Node *curr=head;      while(curr->next!=NULL)      curr=curr->next;            curr->next=temp;      return head;   }};
0
amiransarimy1 month ago
Python Solutions
 
def insertAtBegining(self,head,x):
       newNode = Node(x)
       if not head:
           newNode.next = None
       else:
           newNode.next = head
       head = newNode
       return head
   
   #Function to insert a node at the end of the linked list.
   def insertAtEnd(self,head,x):
       newNode = Node(x)
       if not head:
           newNode.next = None
           head = newNode
       else:
           tmp = head
           while tmp.next:
               tmp = tmp.next
           tmp.next = newNode
       return head
0
ahmadsqf1 month ago
class Solution{
  public:
    //Function to insert a node at the beginning of the linked list.
    Node *insertAtBegining(Node *head, int x) {
       Node * newHeadNode = new Node(x);
       newHeadNode -> next = head; 
       
       return newHeadNode; 
    }
    
    
    //Function to insert a node at the end of the linked list.
    Node *insertAtEnd(Node *head, int x)  {
       
       Node * traversalNode = head; 
       Node * newNode = new Node(x);
       newNode -> next = NULL;
       
       if (!head)
            return newNode;
       
       while (traversalNode -> next)
       {
           traversalNode = traversalNode -> next;
       }
       
       traversalNode -> next = newNode; 

       return head; 
    }
};
0
ajinkyahimanshu0071 month ago
Easy to Understand Python Solution
 
def insertAtBegining(self,head,x):       # code here       x = Node(x)       x.next = head       return x      #Function to insert a node at the end of the linked list.   def insertAtEnd(self,head,x):       curr = head       x = Node(x)       if head:           while curr.next:               curr = curr.next           curr.next = x       else:           head = x       return head
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.