Insert a Node at the end of Doubly Linked List
Last Updated :
07 Aug, 2024
Given a Doubly Linked List, the task is to insert a new node at the end of the linked list.
Examples:
Input: Linked List = 1 <-> 2 <-> 3, NewNode = 4
Output: Linked List = 1 <-> 2 <-> 3 <-> 4
Input: Linked List = NULL, NewNode = 1
Output: Linked List = 1
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.
Insert node 4 at the end of Doubly Linked ListSteps 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.
C++
// C++ Program to insert a node at the end of doubly linked list
#include <bits/stdc++.h>
using namespace std;
struct Node {
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 == NULL) {
head = new_node;
}
else {
Node *curr = head;
while (curr->next != NULL) {
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;
}
void printList(Node *head) {
Node *curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a harcoded 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;
// Print the original list
cout << "Original Linked List: ";
printList(head);
// Insert a new node with data 4 at the end
cout << "Inserting Node with data 4 at the end: ";
int data = 4;
head = insertEnd(head, data);
// Print the updated list
printList(head);
return 0;
}
C
// C Program to insert a node at the end of doubly linked list
#include <stdio.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
// Function to create a new node with the given data
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;
return new_node;
}
// Function to insert a new node at the end of the doubly linked list
struct Node* insertEnd(struct Node *head, int new_data) {
struct Node *new_node = createNode(new_data);
// If the linked list is empty, set the new node as the head
if (head == NULL) {
head = new_node;
} else {
struct Node *curr = head;
while (curr->next != NULL) {
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 head;
}
void printList(struct Node *head) {
struct Node *curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
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;
// Print the original list
printf("Original Linked List: ");
printList(head);
// Insert a new node with data 4 at the end
printf("Inserting Node with data 4 at the end: ");
head = insertEnd(head, 4);
// Print the updated list
printList(head);
return 0;
}
Java
// Java Program to insert a node at the end of
// 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 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
public static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
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;
// Print the original list
System.out.println("Original Linked List: ");
printList(head);
// Insert a new node with data 4 at the end
System.out.println(
"Inserting Node with data 4 at the end: ");
int data = 4;
head = insertEnd(head, data);
// Print the updated list
printList(head);
}
}
Python
# Python Program to insert a node at the end of doubly linked list
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 insert_end(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
def print_list(head):
curr = head
while curr is not None:
print(curr.data, 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
# Print the original list
print("Original Linked List: ", end="")
print_list(head)
# Insert a new node with data 4 at the end
print("Inserting Node with data 4 at the end: ", end="")
data = 4
head = insert_end(head, data)
# Print the updated list
print_list(head)
C#
// C# Program to insert a node at the end of doubly linked list
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 doubly linked list
public static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.Data + " ");
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;
// Print the original list
Console.Write("Original Linked List: ");
PrintList(head);
// Insert a new node with data 4 at the end
Console.Write("Inserting Node with data 4 at the end: ");
int data = 4;
head = InsertEnd(head, data);
// Print the updated list
PrintList(head);
}
}
JavaScript
// Javascript Program to insert a node at the end of doubly
// linked list
class Node {
constructor(data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
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 printList(head)
{
let curr = head;
let result = "";
while (curr !== null) {
result += curr.data + " ";
curr = curr.next;
}
console.log(result.trim());
}
// 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;
// Print the original list
console.log("Original Linked List: ");
printList(head);
// Insert a new node with data 4 at the end
console.log("Inserting Node with data 4 at the end: ");
const data = 4;
head = insertEnd(head, data);
// Print the updated list
printList(head);
OutputOriginal Linked List: 1 2 3
Inserting Node with data 4 at the end: 1 2 3 4
Time Complexity: O(N), where N is the number of nodes in the linked list.
Auxiliary Space: O(1)
Similar Reads
Insert Node at the End of a Linked List Given a linked list, the task is to insert a new node at the end of the linked list.Examples:Input: LinkedList = 2 -> 3 -> 4 -> 5, NewNode = 1Output: LinkedList = 2 -> 3 -> 4 -> 5 -> 1Input: LinkedList = NULL, NewNode = 1Output: LinkedList = 1Approach:Â Inserting at the end invol
9 min read
Insert a Node after a given node in Doubly Linked List Given a Doubly Linked List, the task is to insert a new node after a given node in the linked list.Examples: Input: Linked List = 1 <-> 2 <-> 4, newData = 3, key = 2Output: Linked List = 1 <-> 2 <-> 3 <-> 4Explanation: New node 3 is inserted after key, that is node 2.In
11 min read
Insert a Node before a given node in Doubly Linked List Given a Doubly Linked List, the task is to insert a new node before a given node in the linked list.Examples: Input: Linked List = 1 <-> 3 <-> 4, newData = 2, key = 3Output: Linked List = 1 <-> 2 <-> 3 <-> 4Explanation: New node with data 2 is inserted before the node w
12 min read
Insert a Node at a specific position in Doubly Linked List Given a Doubly Linked List, the task is to insert a new node at a specific position in the linked list. Examples:Input: Linked List = 1 <-> 2 <-> 4, newData = 3, position = 3Output: Linked List = 1 <-> 2 <-> 3 <-> 4Explanation: New node with data = 3 is inserted at posi
13 min read
Insert a Node at Front/Beginning of a Linked List Given a linked list, the task is to insert a new node at the beginning/start/front of the linked list.Example:Input: LinkedList = 2->3->4->5, NewNode = 1Output: 1 2 3 4 5Input: LinkedList = 2->10, NewNode = 1Output: 1 2 10Approach:To insert a new node at the front, we create a new node a
8 min read
Insert a node in Linked List before a given node Given a linked list, the task is to insert a new node with a specified value into a linked list before a node with a given key.ExamplesInput: head: 1 -> 2 -> 3 -> 4 -> 5 , newData = 6, key = 2Output: 1 -> 6 -> 2 -> 3 -> 4 -> 5 Explanation: After inserting node with value 6
15+ min read
Insertion in a Doubly Linked List Inserting a new node in a doubly linked list is very similar to inserting new node in linked list. There is a little extra work required to maintain the link of the previous node. In this article, we will learn about different ways to insert a node in a doubly linked list.Table of ContentInsertion a
6 min read
Insert a Node after a given Node in Linked List Given a linked list, the task is to insert a new node after a given node of the linked list. If the given node is not present in the linked list, print "Node not found".Examples:Input: LinkedList = 2 -> 3 -> 4 -> 5, newData = 1, key = 2Output: LinkedList = 2 -> 1 -> 3 -> 4 -> 5I
11 min read
Sorted insert in a doubly linked list with head and tail pointers A Doubly linked list is a linked list that consists of a set of sequentially linked records called nodes. Each node contains two fields that are references to the previous and to the next node in the sequence of nodes. The task is to create a doubly linked list by inserting nodes such that list rema
10 min read
Insertion at the end in circular linked list A circular linked list is a data structure where each node points to the next, and the last node connects back to the first, creating a loop. Insertion at the end in circular linked list is an important operation. Understanding how to perform this insertion is essential for effectively manage and us
7 min read