Write an algorithm to insert a new node at the end of a Singly linked list

inserting a node at the end of a linked list




Linked List | Set 2 (Inserting a node)

We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.
All programs discussed in this post consider the following representations of linked list.




// A linked list node
class Node
{
public:
int data;
Node *next;
};
// This code is contributed by rathbhupendra




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




// Linked List Class
class LinkedList
{
Node head; // head of list
/* Node Class */
class Node
{
int data;
Node next;
// Constructor to create a new node
Node(int d) {data = d; next = null; }
}
}




# Node class
class Node:
# Function to initialize the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null
# Linked List class
class LinkedList:
# Function to initialize the Linked List object
def __init__(self):
self.head = None




/* Linked list Node*/
public class Node
{
public int data;
public Node next;
public Node(int d) {data = d; next = null; }
}




In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list.

Required knowledge

Basic C programming, Functions, Singly Linked List, Dynamic memory allocation

Algorithm to insert node at the end of Singly linked list

Algorithm to insert node at the end of a Singly Linked List Begin: createSinglyLinkedList (head) alloc (newNode) If (newNode == NULL) then write ('Unable to allocate memory') End if Else then read (data) newNode.data ← data newNode.next ← NULL temp ← head While (temp.next != NULL) do temp ← temp.next End while temp.next ← newNode End else End

Linked List Operations: Traverse, Insert and Delete

In this tutorial, you will learn different operations on a linked list. Also, you will find implementation of linked list operations in C/C++, Python and Java.

There are various linked list operations that allow us to perform different actions on linked lists. For example, the insertion operation adds a new element to the linked list.

Here's a list of basic linked list operations that we will cover in this article.

  • Traversal - access each element of the linked list
  • Insertion - adds a new element to the linked list
  • Deletion - removes the existing elements
  • Search - find a node in the linked list
  • Sort - sort the nodes of the linked list

Before you learn about linked list operations in detail, make sure to know about Linked List first.

Things to Remember about Linked List

  • head points to the first node of the linked list
  • next pointer of the last node is NULL, so if the next current node is NULL, we have reached the end of the linked list.

In all of the examples, we will assume that the linked list has three nodes 1 --->2 --->3 with node structure as below:

struct node { int data; struct node *next; };

C Exercises: Insert a new node at the end of a Singly Linked List

Last update on December 20 2021 09:11:06 (UTC/GMT +8 hours)