To insert a new node at the front, we create a new node and point its next reference to the current head of the linked list. Then, we update the head to be this new node. This operation is efficient because it only requires adjusting a few pointers.
Algorithm:
Make the first node of Linked List linked to the new node
Remove the head from the original first node of Linked List
Make the new node as the Head of the Linked List.
Below is the implementation of the approach:
C++
#include<iostream>usingnamespacestd;// Define a node in the linked listclassNode{public:intdata;Node*next;// Constructor to initialize the nodeNode(intx){data=x;next=nullptr;}};// Function to insert a new node// at the beginning of the listNode*insertAtFront(Node*head,intx){Node*newNode=newNode(x);newNode->next=head;returnnewNode;}voidprintList(Node*head){Node*curr=head;while(curr!=nullptr){cout<<curr->data;if(curr->next!=nullptr){cout<<" -> ";}curr=curr->next;}cout<<endl;}intmain(){// Create the linked list 2->3->4->5Node*head=newNode(2);head->next=newNode(3);head->next->next=newNode(4);head->next->next->next=newNode(5);// Insert a new node at the front of the listintx=1;head=insertAtFront(head,x);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>// Define a node in the linked liststructNode{intdata;structNode*next;};// Function to create a new nodestructNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->next=NULL;returnnewNode;}// Function to insert a new node at the beginning of the liststructNode*insertAtFront(structNode*head,intx){structNode*newNode=createNode(x);newNode->next=head;returnnewNode;}// Function to print the linked listvoidprintList(structNode*head){structNode*curr=head;while(curr!=NULL){printf("%d",curr->data);if(curr->next!=NULL)printf(" -> ");curr=curr->next;}printf("\n");}intmain(){// Create the linked list 2->3->4->5structNode*head=createNode(2);head->next=createNode(3);head->next->next=createNode(4);head->next->next->next=createNode(5);// Insert a new node at the frontintx=1;head=insertAtFront(head,x);// Print the updated linked listprintList(head);return0;}
Java
classNode{intdata;Nodenext;// Constructor to initialize the nodeNode(intx){data=x;next=null;}}classGfG{// Function to insert a new node at // the beginning of the liststaticNodeinsertAtFront(Nodehead,intx){NodenewNode=newNode(x);newNode.next=head;returnnewNode;}// Function to print the contents// of the linked liststaticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data);if(curr.next!=null){System.out.print(" -> ");}curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){// Create the linked list 2->3->4->5Nodehead=newNode(2);head.next=newNode(3);head.next.next=newNode(4);head.next.next.next=newNode(5);// Insert a new node at the // front of the listintx=1;head=insertAtFront(head,x);printList(head);}}
Python
# Define a node in the linked listclassNode:def__init__(self,x):self.data=xself.next=None# Function to insert a new node# at the beginning of the listdefinsertAtFront(head,x):newNode=Node(x)newNode.next=headreturnnewNode# Function to print the contents# of the linked listdefprintList(head):curr=headwhilecurrisnotNone:print(curr.data,end="")ifcurr.nextisnotNone:print(" -> ",end="")curr=curr.nextprint()if__name__=="__main__":# Create the linked list 2->3->4->5head=Node(2)head.next=Node(3)head.next.next=Node(4)head.next.next.next=Node(5)# Insert a new node at# the front of the listx=1head=insertAtFront(head,x)# Print the updated listprintList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;// Constructor to initialize the nodepublicNode(intx){data=x;next=null;}}classGfG{// Function to insert a new node// at the beginning of the liststaticNodeinsertAtFront(Nodehead,intx){NodenewNode=newNode(x);newNode.next=head;returnnewNode;}// Function to print the contents// of the linked liststaticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data);if(curr.next!=null){Console.Write(" -> ");}curr=curr.next;}Console.WriteLine();}staticvoidMain(string[]args){// Create the linked list 2->3->4->5Nodehead=newNode(2);head.next=newNode(3);head.next.next=newNode(4);head.next.next.next=newNode(5);// Insert a new node at the front of the listintx=1;head=insertAtFront(head,x);// Print the updated listprintList(head);}}
JavaScript
// Define a node in the linked listclassNode{constructor(x){this.data=x;this.next=null;}}// Function to insert a new node // at the beginning of the listfunctioninsertAtFront(head,x){letnewNode=newNode(x);newNode.next=head;returnnewNode;}// Function to print the contents // of the linked listfunctionprintList(head){letcurr=head;while(curr!==null){process.stdout.write(curr.data.toString());if(curr.next!==null){process.stdout.write(" -> ");}curr=curr.next;}console.log();}// Driver codefunctionmain(){// Create the linked list 2->3->4->5lethead=newNode(2);head.next=newNode(3);head.next.next=newNode(4);head.next.next.next=newNode(5);// Insert a new node at // the front of the listletx=1;head=insertAtFront(head,x);// Print the updated listprintList(head);}main();
Output
1 -> 2 -> 3 -> 4 -> 5
Time Complexity: O(1), We have a pointer to the head and we can directly attach a node and update the head pointer. So, the Time complexity of inserting a node at the head position is O(1). Auxiliary Space: O(1)