VIRTUAL LABS

Logo

Linked List




Algorithm



Creation is initializing an empty linked list or adding a new nodes to form a list. Insertion is adding a new node at the beginning, end, or at specific position in the list. Traversal is Visiting each node in the list to read or display values. Deletion is removing a node from the beginning, end, or by value, adjusting pointers accordingly



Insertion at the end


  • Create a new node (newNode) with given data.
  • If the linked list is empty (head == null), set head = newNode.
  • Otherwise, traverse the linked list to the last node.
  • Set the next pointer of the last node to newNode.
  • newNode → next = null (this marks the new node as the end of the list).



Traversal through the linked list


  • Start with the head node: Initialize a pointer and set it to the head of the linked list.
  • Loop through the list:While current is not NULL: Print or process the data stored in the current node. Move the current pointer to the next node (current = current->next).
  • End of traversal: Once current is NULL, the traversal is complete, indicating that all nodes have been visited.


Deletion by Value


  • If the list is empty, return.
  • If the head node contains the value, update the head pointer to the next node.
  • Otherwise, traverse the list to find the node to delete.
  • Adjust the next pointer of the previous node to remove the node.