Given a linked list, the task is to insert a new node at the beginning/start/front of the linked list.

Insert a new node at the front of the Linked List
Example:
Input: LinkedList = 2->3->4->5, NewNode = 1
Output: LinkedList = 1->2->3->4->5
Input: LinkedList = , NewNode = 1
Output: LinkedList = 1
Approach:
To insert a node at the start/beginning/front of a Linked List, we need to:
- 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.
The below steps should be followed to insert a new node at the front of the linked list
- Allocate a new node (say temp).
- Put the required data into temp.
- The ‘next’ pointer of the node should be pointed to the current head.
- Now make the head pointer point to temp.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void insertAtFront(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* node)
{
while (node != NULL) {
cout << " " << node->data;
node = node->next;
}
cout << "\n" ;
}
int main()
{
Node* head = NULL;
insertAtFront(&head, 1);
insertAtFront(&head, 2);
insertAtFront(&head, 3);
insertAtFront(&head, 4);
insertAtFront(&head, 5);
insertAtFront(&head, 6);
cout << "After inserting Nodes at their front: " ;
printList(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtFront( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* node)
{
while (node != NULL) {
printf ( " %d" , node->data);
node = node->next;
}
printf ( "\n" );
}
int main()
{
struct Node* head = NULL;
insertAtFront(&head, 1);
insertAtFront(&head, 2);
insertAtFront(&head, 3);
insertAtFront(&head, 4);
insertAtFront(&head, 5);
insertAtFront(&head, 6);
printf ( "After inserting nodes at their front: " );
printList(head);
return 0;
}
|
Java
import java.io.*;
class Node {
int data;
Node next;
}
class LinkedList {
Node head;
void insertAtFront( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head;
head = new_node;
}
void printList()
{
Node node = head;
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
System.out.println();
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.insertAtFront( 6 );
list.insertAtFront( 5 );
list.insertAtFront( 4 );
list.insertAtFront( 3 );
list.insertAtFront( 2 );
list.insertAtFront( 1 );
System.out.print(
"After inserting nodes at their front: " );
list.printList();
}
}
|
Python3
class Node:
def __init__( self ):
self .data = None
self . next = None
def insertAtFront(head_ref, new_data):
new_node = Node()
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(node):
while (node ! = None ):
print (node.data, end = " " )
node = node. next
print ( "\n" )
if __name__ = = '__main__' :
head = None
head = insertAtFront(head, 6 )
head = insertAtFront(head, 5 )
head = insertAtFront(head, 4 )
head = insertAtFront(head, 3 )
head = insertAtFront(head, 2 )
head = insertAtFront(head, 1 )
print ( "After inserting nodes at thier front: " , end = "")
printList(head)
|
C#
using System;
class GFG {
public Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public void insertAtFront( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void printList()
{
Node tnode = head;
while (tnode != null ) {
Console.Write(tnode.data + " " );
tnode = tnode.next;
}
Console.WriteLine();
}
public static void Main(String[] args)
{
GFG llist = new GFG();
llist.insertAtFront(6);
llist.insertAtFront(5);
llist.insertAtFront(4);
llist.insertAtFront(3);
llist.insertAtFront(2);
llist.insertAtFront(1);
Console.Write( "After inserting nodes at their front: " );
llist.printList();
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function insertAtFront(head_ref, new_data) {
const new_node = new Node(new_data);
new_node.next = head_ref[0];
head_ref[0] = new_node;
}
function printList(node) {
let current = node;
while (current !== null ) {
console.log( " " + current.data);
current = current.next;
}
console.log( "\n" );
}
function main() {
const head = [ null ];
insertAtFront(head, 1);
insertAtFront(head, 2);
insertAtFront(head, 3);
insertAtFront(head, 4);
insertAtFront(head, 5);
insertAtFront(head, 6);
console.log( "After inserting Nodes at their front:" );
printList(head[0]);
}
main();
|
Output
Created Linked list is: 2 3 4 5 6
After inserting 1 at front: 1 2 3 4 5 6
Explanation: In the above code we have inserted nodes before them and each node is inserted before the already inserted nodes.
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
05 Oct, 2023
Like Article
Save Article