Operations in Singly Linked List | Pseudo Code for all insertions and deletions operations in SLL
Singly Linked List
Each node or cell has one link part that contains the address of next node in that list. The link part of last node contains NULL value which is the indicator of the end of the node. The example is shown in figure below:
Different Operations in SLL(Singly Linked List)
- Insertion: There are basically three operations related to insertion in linked list.
- Insert at the beginning:
The simple process for inserting node at beginning is that we will first set the reference of existing head node to our current node and then the current node is then set to head. The time complexity for this operation i.e the time complexity to insert a node at the beginning or start will be O(1).
Pseudo Code to insert at beginning:
addAtFirst(node) node.next = head // set reference of current head head = node //set the head to our node
- Insert at the end:
In order to add an element at the end, we must first set the current last node reference to the node we are adding, then set the reference or link of the node we are adding to NULL, and then we must set the last node reference to our node. The time complexity for this operation i.e the time complexity to insert a node at the end will be O(1).
Pseudo Code to insert at end:
addAtLast(node) last.next = node node.next = null last = node
- Insert at the specific position:
In order to add an element at specific position, you must follow these three steps in linked list:
- Start from head and go just before the position where you want to add that specific node.
- Set your node’s reference or link to the next position.
- Then, set the previous node’s reference to your current node.
The time complexity for this operation i.e the time complexity to insert a node at the specific position will be O(n).
Pseudo Code to insert at specific position:
addAtSpecificPosition(node, position) Node temp = head for i=1 to position -1; do temp = temp.next node.next = temp.next temp.next = node
- Insert at the beginning:
- Deletion: The three main operations of deletion in Single Linked List are
- Delete at beginning:
The simple task to delete the node at beginning will be to setting the head to second node. The time complexity for this operation i.e the time complexity to delete a node at the start or beginning position will be O(1).
Pseudo Code:
deleteStart() head = head.next //free() or malloc()
- Delete at end: The steps to delete the last node are:
- Start from head and go upto second last position.
- Set the reference of that second last position to null.
- Free up that location
The time complexity for this operation i.e the time complexity to delete a node at the end or last position will be O(n).
Pseudo Code:
deleteEnd() Node temp = head while temp.next != last temp = temp.next temp.next = null last = temp //free() or malloc()
- Delete at specific position: The steps to delete the node at specific position are:
- Start from head and go to just before that position.
- Set the reference of that position to another next node. If you are at 5th node and you want to delete 6th node then set the reference of 7th node in the 5th node.
- Free up that location
The time complexity for this operation i.e the time complexity to delete a node at specific position will be O(n).
deleteSpecificNode(position) Node temp = head for i=1 to postion -1, do: temp = temp.next temp.next = temp.next.next //free() or malloc()
The visual representation may look like this:
Thanks!
Here are some references that might help you implement Linked List in different programming languages:
Python: Click_Here
Java: Click_HereI just published another blog article regarding worst case time complexity and bigO notation. You can find out here: Worst Case Complexity .For more DSA Related Topics you can stay tuned here: https://facebook.com/secnep
- Delete at beginning:
Wonderful beat I wish to apprentice while you amend your web site how could i subscribe for a blog web site The account aided me a acceptable deal I had been a little bit acquainted of this your broadcast provided bright clear idea
Fantastic site Lots of helpful information here I am sending it to some friends ans additionally sharing in delicious And of course thanks for your effort