Insert a Node at Front/Beginning of Doubly Linked List

Last Updated : 12 Dec, 2025

Given a Doubly Linked List, insert a new node at the beginning/start/front of the linked list.

Examples:

Input: Linked List = 2 <-> 3 <-> 4 , New Node = 1
Output: 1 <-> 2 <-> 3 <-> 4
Explanation: Node 1 is inserted at the beginning and is the new head of the doubly linked list.

Input: Linked List = NULL, New Node = 10
Output: 10
Explanation: Node 1 is the only node in the doubly linked list.

Approach:

  • To insert a new node at the beginning/front of doubly linked list, we create a new node with its previous pointer as NULL and next pointer to the current head of the doubly linked list.
  • Then, we check if the linked list is not empty then we update the previous pointer of the current head to the new node.
  • Finally, we return the new node as the head of the linked list.
Insertion-at-the-Beginning-in-Doubly-Linked-List

Steps to insert a node at the beginning of doubly linked list:

  • Create a new node, say new_node with the given data and set its previous pointer to null, new_node->prev = NULL.
  • Set the next pointer of new_node to the current head, new_node->next = head.
  • If the linked list is not empty, update the previous pointer of the current head to new_node, head->prev = new_node.
  • Return new_node as the head of the updated linked list.
C++
#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node *next, *prev;

    Node(int new_data) {
        data = new_data;
        next = prev = nullptr;
    }
};

// Function to insert a new node at the front of doubly linked list
Node* insertAtFront(Node* head, int new_data) {

    // Create a new node
    Node* new_node = new Node(new_data);

    // Make next of new node as head
    new_node->next = head;

    // Change prev of head node to new node
    if (head != nullptr)
        head->prev = new_node;

    // Return the new node as the head of the doubly linked list
    return new_node;
}

// Function to print the list in required format
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 a hardcoded doubly linked list:
    // 2 <-> 3 <-> 4
    Node* head = new Node(2);
    head->next = new Node(3);
    head->next->prev = head;
    head->next->next = new Node(4);
    head->next->next->prev = head->next;

    // Insert a new node at the front of the list
    int data = 1;
    head = insertAtFront(head, data);

    printList(head);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// Define the Node structure
struct Node
{
    int data;
    struct Node *next;
    struct Node *prev;
};

// Function to create a new node
struct Node *createNode(int new_data)
{
    struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    new_node->prev = NULL;
    return new_node;
}

// Function to insert a new node at the front of doubly linked list
struct Node *insertAtFront(struct Node *head, int new_data)
{
    struct Node *new_node = createNode(new_data);
    new_node->next = head;

    if (head != NULL)
        head->prev = new_node;

    return new_node; // New head
}

// Function to print the list in required format
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 a hardcoded doubly linked list: 2 <-> 3 <-> 4
    struct Node *head = createNode(2);
    head->next = createNode(3);
    head->next->prev = head;
    head->next->next = createNode(4);
    head->next->next->prev = head->next;

    // Insert a new node at the front
    int data = 1;
    head = insertAtFront(head, data);

    // Print the list
    printList(head);

    // Free memory
    struct Node *temp;
    while (head != NULL)
    {
        temp = head;
        head = head->next;
        free(temp);
    }

    return 0;
}
Java
// Java Program to insert a node at the beginning of a
// doubly linked list

class Node {
    int data;
    Node next, prev;

    Node(int newData) {
        data = newData;
        next = prev = null;
    }
}

public class GfG {

    // Function to insert a new node at the front of
    // doubly linked list
    public static Node insertAtFront(Node head, int newData) {

        // Create a new node
        Node newNode = new Node(newData);

        // Make next of new node as head
        newNode.next = head;

        // Change prev of head node to new node
        if (head != null) {
            head.prev = newNode;
        }

        // Return the new node as the head of the doubly
        // linked list
        return newNode;
    }

    // Function to print the doubly linked list
    public 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 a hardcoded doubly linked list:
        // 2 <-> 3 <-> 4
        Node head = new Node(2);
        head.next = new Node(3);
        head.next.prev = head;
        head.next.next = new Node(4);
        head.next.next.prev = head.next;

        // Insert a new node at the front of the list
        int data = 1;
        head = insertAtFront(head, data);

        // Print the updated list
        printList(head);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None

# Function to insert a new node at the front of the doubly linked list
def insert_at_front(head, new_data):
    # Create a new node
    new_node = Node(new_data)

    # Make next of new node as head
    new_node.next = head

    # Change prev of head node to new node
    if head is not None:
        head.prev = new_node

    # Return the new node as the head of the doubly linked list
    return new_node

# Function to print the doubly linked list in required format
def print_list(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 a hardcoded doubly linked list: 2 <-> 3 <-> 4
    head = Node(2)
    head.next = Node(3)
    head.next.prev = head
    head.next.next = Node(4)
    head.next.next.prev = head.next

    # Insert a new node at the front of the list
    data = 1
    head = insert_at_front(head, data)

    # Print the updated list
    print_list(head)
C#
using System;

class Node {
    public int Data;
    public Node Next, Prev;

    public Node(int newData) {
        Data = newData;
        Next = Prev = null;
    }
}

class GfG {

    // Function to insert a new node at the front of
    // doubly linked list
    public static Node insertAtFront(Node head, int newData) {

        // Create a new node
        Node newNode = new Node(newData);

        // Make next of new node as head
        newNode.Next = head;

        // Change prev of head node to new node
        if (head != null) {
            head.Prev = newNode;
        }

        // Return the new node as the head of the doubly
        // linked list
        return newNode;
    }

    // Function to print the doubly linked list
    public 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() {

        // Create a hardcoded doubly linked list:
        // 2 <-> 3 <-> 4
        Node head = new Node(2);
        head.Next = new Node(3);
        head.Next.Prev = head;
        head.Next.Next = new Node(4);
        head.Next.Next.Prev = head.Next;

        // Insert a new node at the front of the list
        int data = 1;
        head = insertAtFront(head, data);

        // Print the updated list
        PrintList(head);
    }
}
JavaScript
// JavaScript Program to insert a node at the beginning of doubly
// linked list

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
        this.prev = null;
    }
}

// Function to insert a new node at the front of the doubly linked list
function insertAtFront(head, newData) {
    // Create a new node
    let newNode = new Node(newData);

    // Make next of new node as head
    newNode.next = head;

    // Change prev of head node to new node
    if (head !== null) {
        head.prev = newNode;
    }

    // Return the new node as the head of the doubly linked list
    return newNode;
}

// Function to print the doubly linked list in required format
function printList(head) {
    let curr = head;
    let result = "";
    while (curr !== null) {
        result += curr.data;
        if (curr.next !== null) {
            result += " <-> ";
        }
        curr = curr.next;
    }
    console.log(result);
}

// Driver Code
// Create a hardcoded doubly linked list:
// 2 <-> 3 <-> 4 -> NULL
let head = new Node(2);
head.next = new Node(3);
head.next.prev = head;
head.next.next = new Node(4);
head.next.next.prev = head.next;

// Insert a new node at the front of the list
head = insertAtFront(head, 1);

// Print the updated list
printList(head);

Output
1 <-> 2 <-> 3 <-> 4

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment