Open In App

Insert a Node at the end of Doubly Linked List

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

Insertion-at-the-End-in-Doubly-Linked-List
Insert node 4 at the end of Doubly Linked 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.
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 Java Python C# JavaScript

Output
Original 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)


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg