Insert a Node at Front of a Linked List (original) (raw)
Last Updated : 21 Oct, 2025
Given a head of the linked list, Insert a new node at the beginning/start/front of the linked list.
**Example:
**Input: x = 1
Output: 1 -> 2 -> 3 -> 4 -> 5
Explanation: We can see that 1 is inserted at the beginning of the linked list.**
**Input: x = 1
**Output: 1 -> 2 -> 10
**Explanation: We can see that 1 is inserted at the beginning of the linked list.
**Approach:
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 using namespace std;
// Define a node in the linked list class Node { public: int data; Node* next;
// Constructor to initialize the node
Node(int x) {
data = x;
next = nullptr;
}};
// Function to insert a new node // at the beginning of the list Node* insertAtFront(Node* head, int x) { Node* newNode = new Node(x); newNode->next = head; return newNode; }
void printList(Node* head) { Node* curr = head;
while (curr != nullptr) {
cout << curr->data;
if (curr->next != nullptr) {
cout << " -> ";
}
curr = curr->next;
}
cout << endl;}
int main() { // Create the linked list 2->3->4->5 Node* head = new Node(2); head->next = new Node(3); head->next->next = new Node(4); head->next->next->next = new Node(5);
// Insert a new node at the front of the list
int x = 1;
head = insertAtFront(head, x);
printList(head);
return 0;}
C
#include <stdio.h> #include <stdlib.h>
// Define a node in the linked list struct Node { int data; struct Node *next; };
// Function to create a new node struct Node *createNode(int x) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = x; newNode->next = NULL; return newNode; }
// Function to insert a new node at the beginning of the list struct Node *insertAtFront(struct Node *head, int x) { struct Node *newNode = createNode(x); newNode->next = head; return newNode; }
// Function to print the linked list void printList(struct Node *head) { struct Node *curr = head; while (curr != NULL) { printf("%d", curr->data); if (curr->next != NULL) printf(" -> "); curr = curr->next; } printf("\n"); }
int main() { // Create the linked list 2->3->4->5 struct Node *head = createNode(2); head->next = createNode(3); head->next->next = createNode(4); head->next->next->next = createNode(5);
// Insert a new node at the front
int x = 1;
head = insertAtFront(head, x);
// Print the updated linked list
printList(head);
return 0;}
Java
class Node { int data; Node next;
// Constructor to initialize the node
Node(int x) {
data = x;
next = null;
}}
class GfG {
// Function to insert a new node at
// the beginning of the list
static Node insertAtFront(Node head, int x) {
Node newNode = new Node(x);
newNode.next = head;
return newNode;
}
// Function to print the contents
// of the linked list
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data);
if (curr.next != null) {
System.out.print(" -> ");
}
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create the linked list 2->3->4->5
Node head = new Node(2);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(5);
// Insert a new node at the
// front of the list
int x = 1;
head = insertAtFront(head, x);
printList(head);
}}
Python
Define a node in the linked list
class Node: def init(self, x): self.data = x self.next = None
Function to insert a new node
at the beginning of the list
def insertAtFront(head, x): newNode = Node(x) newNode.next = head return newNode
Function to print the contents
of the linked list
def printList(head): curr = head while curr is not None: print(curr.data, end="") if curr.next is not None: print(" -> ", end="") curr = curr.next print()
if name == "main": # Create the linked list 2->3->4->5 head = 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 list
x = 1
head = insertAtFront(head, x)
# Print the updated list
printList(head)C#
using System;
class Node { public int data; public Node next;
// Constructor to initialize the node
public Node(int x) {
data = x;
next = null;
}}
class GfG {
// Function to insert a new node
// at the beginning of the list
static Node insertAtFront(Node head, int x) {
Node newNode = new Node(x);
newNode.next = head;
return newNode;
}
// Function to print the contents
// of the linked list
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data);
if (curr.next != null) {
Console.Write(" -> ");
}
curr = curr.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Create the linked list 2->3->4->5
Node head = new Node(2);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(5);
// Insert a new node at the front of the list
int x = 1;
head = insertAtFront(head, x);
// Print the updated list
printList(head);
}}
JavaScript
// Define a node in the linked list class Node { constructor(x) { this.data = x; this.next = null; } }
// Function to insert a new node // at the beginning of the list function insertAtFront(head, x) { let newNode = new Node(x); newNode.next = head; return newNode; }
// Function to print the contents // of the linked list function printList(head) { let curr = head; while (curr !== null) { process.stdout.write(curr.data.toString()); if (curr.next !== null) { process.stdout.write(" -> "); } curr = curr.next; } console.log(); }
// Driver code function main() { // Create the linked list 2->3->4->5 let head = new Node(2); head.next = new Node(3); head.next.next = new Node(4); head.next.next.next = new Node(5);
// Insert a new node at
// the front of the list
let x = 1;
head = insertAtFront(head, x);
// Print the updated list
printList(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)



