← Back

Linked Lists

Simulation Speed:1x

Singly Linked List

A singly linked list is a sequence of nodes where each node contains data and a pointer to the next node. The last node points to null. This structure allows for efficient insertion at the beginning and sequential access, but requires traversal to reach specific positions.

1
2
3

Operations Log:

C Code Example:

// Singly Linked List implementation in C
struct Node {
int value;
struct Node* next;
};
// Insert at beginning
struct Node* insertAtStart(struct Node* head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = value;
newNode->next = head;
return newNode;
}
// Insert at end
struct Node* insertAtEnd(struct Node* head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = value;
newNode->next = NULL;
if (head == NULL) return newNode;
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
return head;
}

made by billy | source code | @b9llach