Given a Doubly Linked List, insert a new node at the end of the linked list.
Examples:
Input: Linked List = 1 <-> 2 <-> 3, NewNode = 4
Output: 1 <-> 2 <-> 3 <-> 4Input: Linked List = NULL, NewNode = 1
Output: 1
Try It Yourself
Approach:
Inserting at the end involves traversing the entire list until we reach the last node. We then set the last nodeâs next reference to point to the new node and new node's previous reference to point to the last node. Thus, making the new node the last element in the list.

Steps to insert a new node at the end:
- If the linked list is empty, we set the new node as the head of linked list and return it as the new head of the linked list.
- Otherwise, traverse the entire list until we reach the last node, say curr.
- Then, set the last nodeâs next to new node and new nodeâs prev to last node, making the new node the last element in the list.
#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 end of doubly linked list
Node *insertEnd(Node *head, int new_data)
{
// Create a new node
Node *new_node = new Node(new_data);
// If the linked list is empty, set the new
// node as the head of linked list
if (head == nullptr)
{
head = new_node;
}
else
{
Node *curr = head;
while (curr->next != nullptr)
{
curr = curr->next;
}
// Set the next of last node to new node
curr->next = new_node;
// Set prev of new node to last node
new_node->prev = curr;
}
// Return the head of the doubly linked list
return head;
}
// 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:
// 1 <-> 2 <-> 3
Node *head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
// Insert a new node with data 4 at the end
int data = 4;
head = insertEnd(head, data);
// Print the updated list
printList(head);
return 0;
}
#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 end of doubly linked list
struct Node *insertEnd(struct Node *head, int new_data)
{
struct Node *new_node = createNode(new_data);
if (head == NULL)
{
head = new_node;
}
else
{
struct Node *curr = head;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = new_node;
new_node->prev = curr;
}
return 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: 1 <-> 2 <-> 3
struct Node *head = createNode(1);
head->next = createNode(2);
head->next->prev = head;
head->next->next = createNode(3);
head->next->next->prev = head->next;
// Insert a new node with data 4 at the end
int data = 4;
head = insertEnd(head, data);
// Print the updated list
printList(head);
// Free allocated memory
struct Node *temp;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
return 0;
}
class Node {
int data;
Node next, prev;
Node(int newData) {
data = newData;
next = prev = null;
}
}
class GfG {
// Function to insert a new node at the end of the
// doubly linked list
public static Node insertEnd(Node head, int newData) {
// Create a new node
Node newNode = new Node(newData);
// If the linked list is empty, set the new node as
// the head
if (head == null) {
head = newNode;
} else {
Node curr = head;
while (curr.next != null) {
curr = curr.next;
}
// Set the next of last node to the new node
curr.next = newNode;
// Set the prev of new node to the last node
newNode.prev = curr;
}
return head;
}
// Function to print the doubly
// linked list in required format
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:
// 1 <-> 2 <-> 3
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
// Insert a new node with data 4 at the end
head = insertEnd(head, 4);
printList(head);
}
}
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Function to insert a new node at the
# end of the doubly linked list
def insertEnd(head, new_data):
# Create a new node
new_node = Node(new_data)
# If the linked list is empty, set
# the new node as the head
if head is None:
head = new_node
else:
curr = head
while curr.next is not None:
curr = curr.next
# Set the next of the last node to the new node
curr.next = new_node
# Set the prev of the new node to the last node
new_node.prev = curr
return head
# Function to print the list in required format
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 a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
# Insert a new node with data 4 at the end
head = insertEnd(head, 4)
# Print only the final list
printList(head)
using System;
class Node {
public int Data;
public Node Next;
public Node Prev;
public Node(int data) {
Data = data;
Next = null;
Prev = null;
}
}
class GfG {
// Function to insert a new node at the
// end of the doubly linked list
public static Node insertEnd(Node head, int newData) {
// Create a new node
Node newNode = new Node(newData);
// If the linked list is empty,
// set the new node as the head
if (head == null) {
head = newNode;
}
else {
Node curr = head;
while (curr.Next != null) {
curr = curr.Next;
}
// Set the next of the last
// node to the new node
curr.Next = newNode;
// Set the prev of the new
// node to the last node
newNode.Prev = curr;
}
return head;
}
// Function to print the list in required format
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:
// 1 <-> 2 <-> 3
Node head = new Node(1);
head.Next = new Node(2);
head.Next.Prev = head;
head.Next.Next = new Node(3);
head.Next.Next.Prev = head.Next;
// Insert a new node with data 4 at the end
int data = 4;
head = insertEnd(head, data);
printList(head);
}
}
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
// Function to insert a new node at
// the end of doubly linked list
function insertEnd(head, newData) {
// Create a new node
const newNode = new Node(newData);
// If the linked list is empty,
// set the new node as the head
if (head === null) {
head = newNode;
}
else {
let curr = head;
while (curr.next !== null) {
curr = curr.next;
}
// Set the next of the last node to the new node
curr.next = newNode;
// Set the prev of the new node to the last node
newNode.prev = curr;
}
return head;
}
// Function to print the 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:
// 1 <-> 2 <-> 3
let head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
// Insert a new node with data 4 at the end
const data = 4;
head = insertEnd(head, data);
printList(head);
Output
1 <-> 2 <-> 3 <-> 4
Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(1)