Attaches a new node at the start of the linked list.
O(1)
O(1)
insertAtEnd
Attaches a new node at the end of the linked list.
O(n)
O(1)
insertAtPosition
Attaches a new node at a specific position in the linked list.
O(n)
O(1)
deleteFromBeginning
Removes the head of the linked list and updates the head to the next node.
O(1)
O(1)
deleteFromEnd
Removes the node present at the end of the linked list.
O(n)
O(1)
deleteFromPosition
Removes a node from a given position in the linked list.
O(n)
O(1)
iterate
Prints all the values of the nodes present in the linked list.
O(n)
O(1)
insertAtBeginning
void insertAtBeginning(Node*& head, int value) { Node* newNode = new Node(value); newNode->next = head; head = newNode;}
insertAtEnd
void insertAtEnd(Node*& head, int value){ Node* newNode = new Node(value); if(head == nullptr) { head = newNode; return; } Node* temp = head; //iterator node while(temp->next != nullptr) { // iterate through the list to find the tail node temp = temp->next; } temp->next = newNode; // assign the tail's next to point to our newly inserted node}
insertAtPosition
void insertAtPosition(Node*& head, int position, int value) { Node* newNode = new Node(value); if(position == 0) { // edge case insertAtBeginning(head, value); return; } Node* temp = head; // iterator node for(int i = 1; i < position && temp != nullptr; i++) { temp = temp->next; } if(temp == nullptr) return; // position is out of bounds (invalid) newNode->next = temp->next; temp->next = newNode;}
deleteFromBeginning
void deleteFromBeginning(Node*& head) { if(head == nullptr) return; // if list is empty Node* temp = head; head = head->next; delete temp; //remember to deallocate memory after removing unwanted node}
deleteFromEnd
void deleteFromEnd(Node*& head) { if(head == nullptr) return; //empty list case if(head->next == nullptr) { delete head; head = nullptr; return; } Node* temp = head; // traversal node while(temp->next != nullptr) { // traverse to the second-last node in the list temp = temp->next; } delete temp->next; temp->next = nullptr;}