Open In App

Insert a Node at Front of a Linked List

Last Updated : 05 Oct, 2025
Suggest changes
Share
Like Article
Like
Report

Given a head of the linked list, Insert a new node at the beginning/start/front of the linked list.

Example:

Input: x = 1

111


Output: 1 -> 2 -> 3 -> 4 -> 5
Explanation: We can see that 1 is inserted at the beginning of the linked list.

222-


Input: x = 1

333


Output: 1 -> 2 -> 10
Explanation: We can see that 1 is inserted at the beginning of the linked list.

444


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 <iostream> 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; } 
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)


Article Tags :

Explore