How to Create and Connect Nodes in a Linked List in Various Programming Languages

In this article, we’ll explore how to represent a basic linked list using several popular programming languages. We’ll see how to define a linked list node, create linked list nodes with necessary data, and link them together in a specific sequence.

Below is an example linked list which we’ll try to create programmatically:

A
A
B
B
C
C
Head Node
Head Node
Tail Node
Tail Node
D
D
Text is not SVG - cannot display

If you are new to linked lists, click here to learn the basics of linked lists.

Defining a Linked List Node

Each node in a singly linked list needs enough memory to store two attributes:

  • Value/Data: This attribute holds the actual information or content that the node represents. It can be of any data type, such as an integer, string, or even a complex object, depending on the requirements of the linked list.
  • Pointer/Reference: This attribute stores the memory address (reference) of the next node in the sequence. It acts as a link, connecting the current node to the following one, allowing for traversal through the list. In the case of the last node (tail node), this pointer typically points to null, indicating the end of the list.

Taking into account the above requirements, below is how the definition of a singly linked list node looks like:


class Node:
  def __init__(self, data):
    # Initialize the node with given data
    self.data = data
    # Set the next pointer to None initially
    self.next = None

Creating Linked List Nodes

Let’s say we need to create a linked list to store elements A, B, C, D. We need to create a node for each element and initialize it’s data field with the corresponding element. The next pointers for each node can be set to null as we have not linked the created nodes yet.

Below is how to create and initialize the linked list nodes using various programming languages:


# Create a node for each element
node1 = Node("A")
node2 = Node("B")
node3 = Node("C")
node4 = Node("D")

Notice that in programming languages with manual memory management (like C and C++), we manually delete each node in the linked list after all required operations are complete. Missing this step will introduce memory leaks which causes programs to crash.

Linking all Created Nodes Together

The next step is to connect all the created linked list nodes together in a sequence. This is generally done by using the expression: node1.next=node2. After executing this expression, node1 will point to node2 as the next node in the sequence. Below is how this syntax looks in different programming languages:


# link/chain the nodes in sequence
node1.next = node2
node2.next = node3
node3.next = node4

# node1 is the head node in the linked list
head = node1
# node4 is the tail node in the linked list
tail = node4

Note that once a linked list is created, addresses/references to internal nodes like node2, node3 etc is not stored. Only the head and tail nodes are directly visible/accessible to the users of linked list. All other nodes can be accessed by traversing the linked list using head node.

Combining all the Steps

To summarize, below are all the steps involved in building a linked list:

  • Defining a linked list node
  • Creating a linked list node for each element which needs to be stored
  • Linking all the created nodes in a sequence

Below is the code in various programming languages which includes the above steps:


# Node class to store data and link to the next node
class Node:
  def __init__(self, data):
    # Initialize the node with given data
    self.data = data
    # Set the next pointer to None initially
    self.next = None

# We need to create the nodes first before linking them
node1 = Node("A")
node2 = Node("B")
node3 = Node("C")
node4 = Node("D")

# Let us now link/chain the nodes in order
# to form a linked list
node1.next = node2
node2.next = node3
node3.next = node4

# node1 is the head node in the linked list
head = node1
# node4 is the tail node in the linked list
tail = node4

# let's print all the data using head node
current_node = head
while current_node != None:
  print(current_node.data, end=" ")
  current_node = current_node.next

After linking all the nodes, we also added a block of code to print values present in all the nodes in the linked list. You can go through this article on linked list traversal to learn more about how it works.

Further Reading