VOOZH about

URL: https://www.geeksforgeeks.org/dsa/insert-a-node-at-frontbeginning-of-doubly-linked-list/

⇱ Insert a Node at Front/Beginning of Doubly Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

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

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

Comment
Article Tags: