Understanding Basic Operations in Singly Linked Lists
Learning about singly linked lists is really important for understanding how data structures work. Here’s an easy guide to some common operations you can do with singly linked lists.
A singly linked list is made up of small parts called nodes. Each node usually has two main parts:
Here’s how you might set up a node in Python:
class Node:
def __init__(self, data):
self.data = data
self.next = None
You can add new nodes to your list in different ways:
At the Head: To add a new node at the front, create a new node and link its next
to the current head. Update the head to be this new node.
At the Tail: To add a new node at the end, go to the last node and link it to the new node.
At a Specific Position: To add a node in the middle, first go to the node before where you want to insert. Adjust the pointers so everything connects correctly.
Here’s a simple example to insert at the head:
def insert_at_head(head, new_data):
new_node = Node(new_data)
new_node.next = head
return new_node
You can also remove nodes from your list:
From the Head: To remove the first node, just update the head to the second node.
By Value: To remove a specific value, go through the list, find the node you want to remove, and adjust the pointers to skip over it.
From the Tail: To remove the last node, go to the second to last node and set its next
to None
.
Here's an example of how to delete a node by its value:
def delete_node(head, key):
temp = head
if temp and temp.data == key:
return head.next
while temp.next:
if temp.next.data == key:
temp.next = temp.next.next
return head
temp = temp.next
To see all the values in your list, you can go through it starting from the head. Keep going until you reach the end, where next
is None
. You can print each node's value as you go.
Here’s a simple way to do that:
def traverse(head):
current = head
while current:
print(current.data)
current = current.next
These basic operations make up the foundation of singly linked lists. With these skills, you can use linked lists in many different ways!
Understanding Basic Operations in Singly Linked Lists
Learning about singly linked lists is really important for understanding how data structures work. Here’s an easy guide to some common operations you can do with singly linked lists.
A singly linked list is made up of small parts called nodes. Each node usually has two main parts:
Here’s how you might set up a node in Python:
class Node:
def __init__(self, data):
self.data = data
self.next = None
You can add new nodes to your list in different ways:
At the Head: To add a new node at the front, create a new node and link its next
to the current head. Update the head to be this new node.
At the Tail: To add a new node at the end, go to the last node and link it to the new node.
At a Specific Position: To add a node in the middle, first go to the node before where you want to insert. Adjust the pointers so everything connects correctly.
Here’s a simple example to insert at the head:
def insert_at_head(head, new_data):
new_node = Node(new_data)
new_node.next = head
return new_node
You can also remove nodes from your list:
From the Head: To remove the first node, just update the head to the second node.
By Value: To remove a specific value, go through the list, find the node you want to remove, and adjust the pointers to skip over it.
From the Tail: To remove the last node, go to the second to last node and set its next
to None
.
Here's an example of how to delete a node by its value:
def delete_node(head, key):
temp = head
if temp and temp.data == key:
return head.next
while temp.next:
if temp.next.data == key:
temp.next = temp.next.next
return head
temp = temp.next
To see all the values in your list, you can go through it starting from the head. Keep going until you reach the end, where next
is None
. You can print each node's value as you go.
Here’s a simple way to do that:
def traverse(head):
current = head
while current:
print(current.data)
current = current.next
These basic operations make up the foundation of singly linked lists. With these skills, you can use linked lists in many different ways!