Inserting a Node in a Single Linked List: Methods and Examples

Inserting a Node in a Single Linked List: Methods and Examples

Inserting a node into a singly linked list is a common operation in computer science and can be performed in multiple ways depending on where the new node is to be placed: at the beginning, at the end, or at a specific position. This article provides detailed explanations and Python code examples for each of these scenarios.

1. Inserting a Node at the Beginning

To insert a node at the beginning of the list, you need to create a new node and point it to the current head of the list, then update the head to point to the new node. This ensures that the new node becomes the new head of the list.

Python Code Example

Here is a class implementation for a singly linked list with the method to insert a node at the beginning.

class Node:
    def __init__(self, data):
         data
         None
class LinkedList:
    def __init__(self):
        self.head  None
    def insert_at_beginning(self, new_data):
        new_node  Node(new_data)
        new_  self.head
        self.head  new_node
# Usage example
llist  LinkedList()
_at_beginning(10)
_at_beginning(20)

2. Inserting a Node at the End

To insert a node at the end of the list, you traverse the list to find the last node and append the new node to its next pointer. This method ensures that the new node is added as the last element in the list.

Python Code Example

Below is the code to insert a node at the end of a singly linked list.

def insert_at_end(self, new_data):
    new_node  Node(new_data)
    if self.head is None:
        self.head  new_node
        return
    last  self.head
    while  None:
        last 
    /  new_node
# Usage example
_at_end(30)

3. Inserting a Node at a Specific Position

Inserting a node at a specific position involves traversing the list to the desired position and updating the pointers accordingly. This method is useful when the node needs to be added at any position other than the beginning or the end of the list.

Python Code Example

The process is shown below with a code snippet for inserting a node at a specified position.

def insert_at_position(self, new_data, position):
    new_node  Node(new_data)
    if position  0:
        new_  self.head
        self.head  new_node
        return
    current  self.head
    for i in range(position - 1):
        if current is None:
            raise Exception
        current 
    new_ 
    /  new_node
# Usage example
_at_position(40, 1)

Complete Example

Here is a complete example that includes all three insertion methods:

class Node:
    def __init__(self, data):
         data
         None
class LinkedList:
    def __init__(self):
        self.head  None
    def insert_at_beginning(self, new_data):
        new_node  Node(new_data)
        new_  self.head
        self.head  new_node
    def insert_at_end(self, new_data):
        new_node  Node(new_data)
        if self.head is None:
            self.head  new_node
            return
        last  self.head
        while  None:
            last 
    /  new_node
    def insert_at_position(self, new_data, position):
        new_node  Node(new_data)
        if position  0:
            new_  self.head
            self.head  new_node
            return
        current  self.head
        for i in range(position - 1):
            if current is None:
                raise Exception
            current 
    /  new_node
    def print_list(self):
        current  self.head
        while current:
            print end 
            current 
        print
# Usage example
llist  LinkedList()
_at_end(10)
_at_beginning(20)
_at_position(15, 1)
_list()   Output: 20 15 10

Summary

Here's a summary of each insertion method:

Insert at the Beginning

Update the head to the new node.

Insert at the End

Traverse to the last node and append the new node.

Insert at a Specific Position

Traverse to the specified position and adjust pointers accordingly.

Conclusion

Finding the right place to insert a node in a singly linked list is crucial for efficient data manipulation. The methods described above are widely used and form the basis for more complex linked list operations. If you have any further questions or need additional examples, feel free to ask!