这是用户在 2024-10-27 10:22 为 https://www.geeksforgeeks.org/insert-node-at-the-end-of-a-linked-list/ 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Open In App

Insert Node at the End of a Linked List

Last Updated : 29 Jul, 2024
Summarize
Comments
Improve
Suggest changes
11 Likes
Like
Save
Share
Report
News Follow

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 = 1
Output: LinkedList = 2 -> 3 -> 4 -> 5 -> 1

Input: LinkedList = NULL, NewNode = 1
Output: LinkedList = 1

Solve Problem
Basic
43.96%
2.7L

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, making the new node the last element in the list.

Insertion-at-the-End-of-Singly-Linked-List

Following is the approach to add a new node at the end of the linked list:

  • Create a new node and set its next pointer as NULL since it will be the last node.
  • Store the head reference in a temporary variable
  • If the Linked List is empty, make the new node as the head and return
  • Else traverse till the last node
  • Change the next pointer of the last node to point to the new node

Below is the implementation of the approach:

C++ C
#include <stdio.h>
#include <stdlib.h>

// A linked list node
struct Node {
    int data;
    struct Node* next;
};

// 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;
    return new_node;
}

// Given the head of a list and an int, appends
// a new node at the end and returns the head.
struct Node* append(struct Node* head, int new_data) {
  
    // Create a new node
    struct Node* new_node = createNode(new_data);

    // If the Linked List is empty, make
    // the new node as the head and return
    if (head == NULL) {
        return new_node;
    }

    // Store the head reference in a temporary variable
    struct Node* last = head;

    // Traverse till the last node
    while (last->next != NULL) {
        last = last->next;
    }

    // Change the next pointer of the last node 
    // to point to the new node
    last->next = new_node;

    // Return the head of the list
    return head;
}

// This function prints the contents 
// of the linked list starting from the head
void printList(struct Node* node) {
    while (node != NULL) {
        printf(" %d", node->data);
        node = node->next;
    }
}

// Driver code
int main() {
  
    // Create a hard-coded linked list: 
    // 2 -> 3 -> 4 -> 5 -> 6
    struct Node* head = createNode(2);
    head->next = createNode(3);
    head->next->next = createNode(4);
    head->next->next->next = createNode(5);
    head->next->next->next->next = createNode(6);

    printf("Created Linked list is:");
    printList(head);

    // Example of appending a node at the end
    head = append(head, 1);

    printf("\nAfter inserting 1 at the end:");
    printList(head);

    return 0;
}
Java Python C# JavaScript

Output
Created Linked list is:  2 3 4 5 6
After inserting 1 at the end:  2 3 4 5 6 1

Time Complexity: O(N) where N is the length of the linked list
Auxiliary Space: O(1)


Unlock a distraction-free, high-quality learning experience with GeeksforGeeks Premium! Get unlimited access to 35+ expert-led tech courses covering everything from programming languages, DSA to Web Development and Data Science, all designed to help you ace any interview.

Upgrade today and enjoy a 1-year free extension with your subscription, plus exclusive perks like unlimited article summarization, a 100% ad-free environment, AI-powered assistance in coding problems, and so much more. Go Premium!


Similar Reads

Insert a Node at the end of Doubly Linked List
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 = 4Output: Linked List = 1 <-> 2 <-> 3 <-> 4 Input: Linked List = NULL, NewNode = 1Output: Linked List = 1 Approach: Inserting at the end involves traversing the entire list
9 min read
Insert a node after the n-th node from the end
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 <= n. Examples: Input : list: 1->3->4->5 n = 4, x = 2 Output : 1->2->3->4->5 4th node from the end is 1 and insertion has been done after this node. Input : list: 10->8
15+ 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 -> 5 Input: LinkedList = 1 -> 3 -> 5 -> 7, n
11 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. Input: Linked List = 1 <-> 2, newData = 4, k
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 with data = 3 Input: Linked List = 2 <-> 3,
12 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. Examples Input: head: 1 -> 2 -> 3 -> 4 -> 5 , newData = 6, key = 2Output: 1 -> 6 -> 2 -> 3 -> 4 -> 5 Explanation: After inserting node with value 6 before (key = 2) of the linked list, the result
15+ min read
Insert a linked list into another linked list
Given two linked lists, head1 and head2 of sizes m and n respectively. The task is to remove head1's nodes from the ath node to the bth node and insert head2 in their place. Examples: Input: a = 3, b = 4head1: 10 -> 11 -> 12 -> 13 -> 14 -> 15, head2: 100 -> 101 -> 102 -> 103Output: 10 -> 11 -> 12 -> 100 -> 101 -
10 min read
Swap Kth node from beginning with Kth node from end in a Doubly Linked List
Prerequisites: Doubly Linked List Given a doubly-linked list, the task is to swap Kth node from the beginning with Kth node from the ending.Note: Please note here the nodes are swapped and not the data in the nodes. Examples: Input: DLL = 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6, K = 3 Output: 1 2 4 3 5 6 Explanation: Third nod
15+ min read
Swap Kth node from beginning with Kth node from end in a Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details like Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
15+ min read
Javascript Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
How to insert a Node in a Singly Linked List at a given Position using Recursion
Given a singly linked list as list, a position, and a node, the task is to insert that element in the given linked list at a given position using recursion. Examples: Input: list = 1->2->3->4->5->6->7, node = (val=100,next=null), position = 4 Output: 1->2->3->100->4->5->6->7Explanation: Here the node with valu
14 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 5 Input: LinkedList = 2->10, NewNode = 1Output: 1 2 10 Approach: To insert a new node at the front, we create a new node and point its next reference to the current he
8 min read
Insert a Node at Front/Beginning of Doubly Linked List
Given a Doubly Linked List, the task is to insert a new node at the beginning/start/front of the linked list. Examples: Input: Linked List = 2 <-> 3 <-> 4 -> NULL , New Node = 1Output: 1 <-> 2 <-> 3 <-> 4 -> NULL Explanation: Node 1 is inserted at the beginning and is the new head of the doubly linked list. Input
9 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 position 3 Input: Linked List = 2 <-> 3, newDa
13 min read
Insert node into the middle of the linked list
Given a linked list containing n nodes. The problem is to insert a new node with data x in the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input: LinkedList = 1->2->4 , x = 3Output: 1->2->3->4 Input: LinkedList = 10->20->40->5
15 min read
Insert a node at a specific position in a linked list
Given a singly linked list, a position pos, and data, the task is to insert that data into a linked list at the given position. Examples: Input: 3->5->8->10, data = 2, pos = 2Output: 3->2->5->8->10 Input: 3->5->8->10, data = 11, pos = 5Output: 3->5->8->10->11 [Expected Approach] Using Iterative Method - O(n
8 min read
Recursive Approach to find nth node from the end in the linked list
Find the nth node from the end in the given linked list using a recursive approach. Examples: Input : list: 4->2->1->5->3 n = 2 Output : 5 Algorithm: findNthFromLast(head, n, count, nth_last) if head == NULL then return findNthFromLast(head->next, n, count, nth_last) count = count + 1 if count == n then nth_last = head findNthFromLas
8 min read
Cpp14 Program For Printing Nth Node From The End Of A Linked List (Duplicate)
Given a Linked List and a number n, write a function that returns the value at the n'th node from the end of the Linked List.For example, if the input is below list and n = 3, then output is "B" Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method 1 (Use length of linked list) 1) Calculate the length of Linked
5 min read
Move the Kth Prime Number Node to the End of the Linked List
Given a singly linked list, the task is to move the Kth prime number node to the end of the list while preserving the order of other nodes. Examples: Input: 4 -> 7 -> 11 -> 3 -> 14 -> 2 -> NULL, K = 2Output: 4 -> 7 -> 3 -> 14 -> 2 -> 11 -> NULLExplanation: In the given list, the prime number nodes are 7, 11, 3, a
13 min read
Move the Kth Largest Fibonacci Number Node to the End of a Singly Linked List
Given a singly linked list containing integer values. The task is to find the Kth largest Fibonacci number within this list and move it to the end of the list. Examples: Input: 12 -> 11 -> 0 -> 5 -> 8 -> 13 -> 17 -> 21 -> NULL, K = 3Output: 12 -> 11 -> 0 -> 5 -> 13 -> 17 -> 21 -> 8 -> NULLExplanation:
12 min read
XOR Linked List - Find Nth Node from the end
Given a XOR linked list and an integer N, the task is to print the Nth node from the end of the given XOR linked list. Examples: Input: 4 –> 6 –> 7 –> 3, N = 1 Output: 3 Explanation: 1st node from the end is 3.Input: 5 –> 8 –> 9, N = 4 Output: Wrong Input Explanation: The given Xor Linked List contains only 3 nodes. Approach: Follow
15+ min read
Delete Nth node from the end of the given linked list
Given a linked list and an integer N, the task is to delete the Nth node from the end of the given linked list. Examples: Input: 2 -> 3 -> 1 -> 7 -> NULL, N = 1 Output: The created linked list is: 2 3 1 7 The linked list after deletion is: 2 3 1 Input: 1 -> 2 -> 3 -> 4 -> NULL, N = 4 Output: The created linked list is: 1 2 3
13 min read
Deletion at end (Removal of last node) in a Linked List
Given a linked list, the task is to delete the last node of the given linked list. Examples:   Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULLOutput: 1 -> 2 -> 3 -> 4 -> NULL Explanation: The last node of the linked list is 5, so 5 is deleted. Input: 3 -> 12 -> 15-> NULLOutput: 3 -> 12 -> NULL Explanation: The last no
8 min read
Program for Nth node from the end of a Linked List
Given a Linked List of M nodes and a number N, find the value at the Nth node from the end of the Linked List. If there is no Nth node from the end, print -1. Examples: Input: 1 -> 2 -> 3 -> 4, N = 3Output: 2Explanation: Node 2 is the third node from the end of the linked list. Input: 35 -> 15 -> 4 -> 20, N = 4Output: 35Explanatio
15 min read
Deletion at end (Removal of last node) in a Doubly Linked List
Given a doubly linked list, the task is to delete the last node of the given linked list. Examples: Input: 1 <-> 2 <-> 3 <-> NULLOutput: 1 <-> 2 <-> NULLExplanation: The last node of the linked list is 3, so 3 is deleted. Input: 15 -> NULLOutput: NULLExplanation: The last node of the linked list is 15, so 15 is dele
7 min read
Remove Nth node from end of the Linked List
Given a linked list. The task is to remove the Nth node from the end of the linked list. Examples: Input : LinkedList = 1 ->2 ->3 ->4 ->5 , N = 2Output : 1 ->2 ->3 ->5Explanation: Linked list after deleting the 2nd node from last which is 4, is 1 ->2 ->3 ->5 Input : LinkedList = 7 ->8 ->4 ->3 ->2 , N = 1 Ou
15+ min read
Skip List - Efficient Search, Insert and Delete in Linked List
A skip list is a data structure that allows for efficient search, insertion and deletion of elements in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is determined through a probabilistic analysis. In a skip list, elements are organized in layers, with each layer having a smaller number of elements th
6 min read
XOR linked list- Remove first node of the linked list
Given an XOR linked list, the task is to remove the first node of the XOR linked list. Examples: Input: XLL = 4 < – > 7 < – > 9 < – > 7 Output: 7 < – > 9 < – > 7 Explanation: Removing the first node of the XOR linked list modifies XLL to 7 < – > 9 < – > 7 Input: XLL = NULL Output: List Is Empty Approach: Th
11 min read
XOR Linked List: Remove last node of the Linked List
Given an XOR linked list, the task is to delete the node at the end of the XOR Linked List. Examples: Input: 4<–>7<–>9<–>7Output: 4<–>7<–>9Explanation: Deleting a node from the end modifies the given XOR Linked List to 4<–>7<–>9 Input: 10Output: List is emptyExplanation: After deleting the only node present
15+ min read
Create new linked list from two given linked list with greater element at each node
Given two linked list of the same size, the task is to create a new linked list using those linked lists. The condition is that the greater node among both linked list will be added to the new linked list.Examples: Input: list1 = 5->2->3->8list2 = 1->7->4->5Output: New list = 5->7->4->8Input: list1 = 2->8->9->3li
8 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg