A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/dsa/singly-linked-list-tutorial/ below:

Singly Linked List Tutorial - GeeksforGeeks

Singly Linked List Tutorial

Last Updated : 23 Jul, 2025

A singly linked list is a fundamental data structure, it consists of nodes where each node contains a data field and a reference to the next node in the linked list. The next of the last node is null, indicating the end of the list. Linked Lists support efficient insertion and deletion operations.

Understanding Node Structure

In a singly linked list, each node consists of two parts: data and a pointer to the next node. This structure allows nodes to be dynamically linked together, forming a chain-like sequence.

C++
// Definition of a Node in a singly linked list
struct Node {
  
    // Data part of the node
    int data;

    // Pointer to the next node in the list
    Node* next;

    // Constructor to initialize the node with data
    Node(int data)
    {
        this->data = data;
        this->next = nullptr;
    }
};
C
// Definition of a Node in a singly linked list
struct Node {
    int data;          
    struct Node* next;
};

// Function to create a new Node
struct Node* newNode(int data) {
    struct Node* temp = 
      (struct Node*)malloc(sizeof(struct Node));
    temp->data = data;
    temp->next = NULL;
    return temp;
}
Java
// Definition of a Node in a singly linked list
public class Node {
    int data;
    Node next;

    // Constructor to initialize the node with data
    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}
Python
# Definition of a Node in a singly linked list
class Node:
    def __init__(self, data):
       # Data part of the node
        self.data = data   
        self.next = None    
JavaScript
// Definition of a Node in a singly linked list
class Node {
    constructor(data) {
    // Data part of the node
        this.data = data;   
        this.next = null;   
    }
}

In this example, the Node class contains an integer data field (data) to store the information and a pointer to another Node (next) to establish the link to the next node in the list.

1. Traversal of Singly Linked List

Traversal in a linked list means visiting each node and performing operations like printing or processing data.

Step-by-step approach:

  1. Initialize a pointer (current) to the head of the list.
  2. Loop through the list using a while loop until current becomes NULL.
  3. Process each node (e.g., print its data).
  4. Move to the next node by updating current = current->next.

To read more about Traversal Operation in linked list Refer, Traversal of Singly Linked List

2. Searching in Singly Linked List

Searching in a Singly Linked List refers to the process of looking for a specific element or value within the elements of the linked list.

Step-by-step approach:

  1. Start from the head of the linked list.
  2. Check each node’s data:
  3. Repeat until the end (NULL) is reached.
  4. If no match is found, return false.

To read more about Searching Operation in linked list Refer, Search an element in a Linked List

3. Length of Singly Linked List

Finding the length of a Singly Linked List means counting the total number of nodes.

Step-by-step approach:

  1. Initialize a counter (length = 0).
  2. Start from the head, assign it to current.
  3. Traverse the list:
  4. Return the final length when current becomes NULL.

To read more about Finding Length of linked list Refer, Find Length of a Linked List

4. Insertion in Singly Linked List

Insertion is a fundamental operation in linked lists that involves adding a new node to the list. There are several scenarios for insertion:

a. Insertion at the Beginning of Singly Linked List: Insertion at the beginning involves adding a new node before the current head, making it the new head.

Insert a Node at the Front/Beginning of Linked List

Step-by-step approach:

To read more about Insertion at the Beginning of linked list Refer, Insert a Node at Front/Beginning of a Linked List

b. Insertion at the End of Singly Linked List: To insert a node at the end of the list, traverse the list until the last node is reached, and then link the new node to the current last node

Insertion at end of Linked List

Step-by-step approach:

To read more about Insertion at the end of linked list Refer, Insert Node at the End of a Linked List

c. Insertion at a Specific Position of the Singly Linked List: To insert a node at a specific position, traverse the list to the desired position, link the new node to the next node, and update the links accordingly.

Step-by-step approach:

To read more about Insertion at the specific position of linked list Refer, Insert a node at a specific position in a linked list

5. Deletion in Singly Linked List

Deletion involves removing a node from the linked list. Similar to insertion, there are different scenarios for deletion:

a. Deletion at the Beginning of Singly Linked List: To delete the first node, update the head to point to the second node in the list.

Deletion at beginning in a Linked List

Steps-by-step approach:

To read more about Deletion at the beginning in linked list Refer, Deletion at beginning (Removal of first node) in a Linked List

b. Deletion at the End of Singly Linked List: To delete the last node, traverse the list until the second-to-last node and update its next field to None.

Deletion at the end of linked list

Step-by-step approach:

To read more about Deletion at the end in linked list Refer, Deletion at end (Removal of last node) in a Linked List

c. Deletion at a Specific Position of Singly Linked List: To delete a node at a specific position, traverse the list to the desired position, update the links to bypass the node to be deleted.

Delete a Linked List node at a given position

Step-by-step approach:

To read more about Deletion at the specific position in linked list Refer, Delete a Linked List node at a given position

6. Modify a Singly Linked List

Updating in a Singly Linked List means modifying the value of a node at a given position.

Step-by-step approach:

To read more about Updation Operation in Linked List Refer, Modify contents of Linked List

7. Reversing a Singly Linked List

Reversing a singly linked list means changing the direction of pointers so that the last node becomes the new head.

Step-by-step approach:

To read more about Reversal Operation in Linked List Refer, Reverse a Linked List



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4